pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Ribe <scott_ribe@elevated-dev.com>
To: Michael Lewis <mlewis@entrata.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: query, probably needs window functions
Date: Sat, 23 May 2020 15:46:31 -0600
Message-ID: <67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com> (raw)
In-Reply-To: <CAHOFxGpwuPCSmuSiD9yKDG_rTbvmU156LX3ckRdEo5ixy-NM9g@mail.gmail.com>
References: <037A65BF-4CFE-4EC0-A037-173C9DDF2A9C@elevated-dev.com>
	<CAKFQuwaxjJpoUNtNKzDDg0ujJP9Qr+UL2baug8kuFmd4EPnnng@mail.gmail.com>
	<CAHOFxGpwuPCSmuSiD9yKDG_rTbvmU156LX3ckRdEo5ixy-NM9g@mail.gmail.com>

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




view thread (5+ messages)

Message-ID: <67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com>
Permalink:  ../67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com/
Also on:    postgresql.org/message-id/67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: scott_ribe@elevated-dev.com, mlewis@entrata.com, david.g.johnston@gmail.com
  Subject: Re: query, probably needs window functions
  In-Reply-To: <67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com>
  References: <037A65BF-4CFE-4EC0-A037-173C9DDF2A9C@elevated-dev.com> <CAKFQuwaxjJpoUNtNKzDDg0ujJP9Qr+UL2baug8kuFmd4EPnnng@mail.gmail.com> <CAHOFxGpwuPCSmuSiD9yKDG_rTbvmU156LX3ckRdEo5ixy-NM9g@mail.gmail.com> <67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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