pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedquery, probably needs window functions
5+ messages / 3 participants
[nested] [flat]
* query, probably needs window functions
@ 2020-05-22 18:30 Scott Ribe <scott_ribe@elevated-dev.com>
0 siblings, 1 reply; 5+ messages in thread
From: Scott Ribe @ 2020-05-22 18:30 UTC (permalink / raw)
To: pgsql-general
given, let's say:
create table person (id int not null, name varchar);
create table phone (id int not null, person_id int not null, number varchar);
select person.*, phone.number from person join phone on (person.id = phone.person_id) order by...
How would you get results where only the first row for a person was filled in, with rest of that person's phones showing blanks for those columns? I'm guessing that window functions provide this capability, but I don't know how.
--
Scott Ribe
scott_ribe@elevated-dev.com
https://www.linkedin.com/in/scottribe/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: query, probably needs window functions
@ 2020-05-22 18:34 David G. Johnston <david.g.johnston@gmail.com>
parent: Scott Ribe <scott_ribe@elevated-dev.com>
0 siblings, 1 reply; 5+ messages in thread
From: David G. Johnston @ 2020-05-22 18:34 UTC (permalink / raw)
To: Scott Ribe <scott_ribe@elevated-dev.com>; +Cc: pgsql-general
On Friday, May 22, 2020, Scott Ribe <scott_ribe@elevated-dev.com> wrote:
> given, let's say:
>
> create table person (id int not null, name varchar);
> create table phone (id int not null, person_id int not null, number
> varchar);
>
> select person.*, phone.number from person join phone on (person.id =
> phone.person_id) order by...
>
> How would you get results where only the first row for a person was filled
> in, with rest of that person's phones showing blanks for those columns? I'm
> guessing that window functions provide this capability, but I don't know
> how.
>
>
If a left join doesn’t give you the answer you want you should probably
provide exact input and output data that you are working with/toward.
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: query, probably needs window functions
@ 2020-05-22 19:37 Michael Lewis <mlewis@entrata.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Michael Lewis @ 2020-05-22 19:37 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Scott Ribe <scott_ribe@elevated-dev.com>; 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;
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: query, probably needs window functions
@ 2020-05-22 20:10 David G. Johnston <david.g.johnston@gmail.com>
parent: Michael Lewis <mlewis@entrata.com>
1 sibling, 0 replies; 5+ messages in thread
From: David G. Johnston @ 2020-05-22 20:10 UTC (permalink / raw)
To: Michael Lewis <mlewis@entrata.com>; +Cc: Scott Ribe <scott_ribe@elevated-dev.com>; pgsql-general
On Fri, May 22, 2020 at 12:38 PM Michael Lewis <mlewis@entrata.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.
>
This. I misread the question. You might also consider just outputting one
row per person and output the related phone numbers using
string_agg(phone.number, E'\n')
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: query, probably needs window functions
@ 2020-05-23 21:46 Scott Ribe <scott_ribe@elevated-dev.com>
parent: Michael Lewis <mlewis@entrata.com>
1 sibling, 0 replies; 5+ messages in thread
From: Scott Ribe @ 2020-05-23 21:46 UTC (permalink / raw)
To: Michael Lewis <mlewis@entrata.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general
> On May 22, 2020, at 1:37 PM, Michael Lewis <mlewis@entrata.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;
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2020-05-23 21:46 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-22 18:30 query, probably needs window functions Scott Ribe <scott_ribe@elevated-dev.com>
2020-05-22 18:34 ` David G. Johnston <david.g.johnston@gmail.com>
2020-05-22 19:37 ` Michael Lewis <mlewis@entrata.com>
2020-05-22 20:10 ` David G. Johnston <david.g.johnston@gmail.com>
2020-05-23 21:46 ` Scott Ribe <scott_ribe@elevated-dev.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox