Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jcbyi-0008Jo-1z for pgsql-general@arkaria.postgresql.org; Sat, 23 May 2020 21:46:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jcbye-0007On-RT for pgsql-general@arkaria.postgresql.org; Sat, 23 May 2020 21:46:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jcbye-0007Og-JK for pgsql-general@lists.postgresql.org; Sat, 23 May 2020 21:46:44 +0000 Received: from mailout.easymail.ca ([64.68.200.34]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jcbyY-0003cQ-Na for pgsql-general@postgresql.org; Sat, 23 May 2020 21:46:42 +0000 Received: from localhost (localhost [127.0.0.1]) by mailout.easymail.ca (Postfix) with ESMTP id 0FBA220994; Sat, 23 May 2020 21:46:36 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at emo06-pco.easydns.vpn Received: from mailout.easymail.ca ([127.0.0.1]) by localhost (emo06-pco.easydns.vpn [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wLHYrx_vAfvL; Sat, 23 May 2020 21:46:35 +0000 (UTC) Received: from [192.168.2.16] (unknown [8.39.228.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailout.easymail.ca (Postfix) with ESMTPSA id 9161B22D02; Sat, 23 May 2020 21:46:32 +0000 (UTC) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Subject: Re: query, probably needs window functions From: Scott Ribe In-Reply-To: Date: Sat, 23 May 2020 15:46:31 -0600 Cc: "David G. Johnston" , "pgsql-general@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <67A28341-97F2-437B-9268-DFBDD7D3F12F@elevated-dev.com> References: <037A65BF-4CFE-4EC0-A037-173C9DDF2A9C@elevated-dev.com> To: Michael Lewis X-Mailer: Apple Mail (2.3608.80.23.2.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On May 22, 2020, at 1:37 PM, Michael Lewis wrote: >=20 > 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 =3D 1 then id end AS id,=20 case when row_number =3D 1 then name end as name, phone.number from( select person.id, person.name, phone.number,=20 row_number() over partition by( phone.person_id order by = phone.number ) as row_number from person join phone on person.id =3D phone.person_id ) AS sub order by name, row_number;=