Re: query, probably needs window functions - Mailing list pgsql-general

From Michael Lewis
Subject Re: query, probably needs window functions
Date
Msg-id CAHOFxGpwuPCSmuSiD9yKDG_rTbvmU156LX3ckRdEo5ixy-NM9g@mail.gmail.com
Whole thread Raw
In response to Re: query, probably needs window functions  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: query, probably needs window functions  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: query, probably needs window functions  (Scott Ribe <scott_ribe@elevated-dev.com>)
List pgsql-general
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.

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() 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;

pgsql-general by date:

Previous
From: "Andrus"
Date:
Subject: Re: Query returns no rows in pg_basebackup cluster
Next
From: "David G. Johnston"
Date:
Subject: Re: query, probably needs window functions