Re: query, probably needs window functions

From: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
To: Michael Lewis <mlewis(at)entrata(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: query, probably needs window functions
Date: 2020-05-23 21:46:31
Message-ID: 67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On May 22, 2020, at 1:37 PM, Michael Lewis <mlewis(at)entrata(dot)com> wrote:
>
> I believe something like this is what you want. You might be able to do it without a sub-query by comparing the current name value to the lag value and null it out if it's the same.
> ...

Thanks, that's what I needed! (And better than using lag in my case because there's 4 tables in the joins and many more columns involved. The repetition of "case when lag(...)..." would be really noisy, but it's good to know of that possibility anyway.)

One correction, just for posterity if someone else searches this question, the answer was missing "over", should have been:

select
case when row_number = 1 then id end AS id,
case when row_number = 1 then name end as name,
phone.number
from(
select person.id, person.name, phone.number,
row_number() over partition by( phone.person_id order by phone.number ) as row_number
from person
join phone on person.id = phone.person_id
) AS sub
order by name, row_number;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Bottaro 2020-05-24 16:57:27 Help with streaming replication protocol
Previous Message Richard Suematsu 2020-05-23 18:10:26 Re: libgeotiff missing