Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bcT9z-0001NO-Rd for pgsql-sql@arkaria.postgresql.org; Wed, 24 Aug 2016 08:03:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bcT9z-0005zf-5R for pgsql-sql@arkaria.postgresql.org; Wed, 24 Aug 2016 08:03:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bcT9y-0005zY-Nc for pgsql-sql@postgresql.org; Wed, 24 Aug 2016 08:03:42 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bcT9w-0008ML-TZ for pgsql-sql@postgresql.org; Wed, 24 Aug 2016 08:03:42 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1bcT9s-0003SF-Uv for pgsql-sql@postgresql.org; Wed, 24 Aug 2016 10:03:36 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: Display group title only at the first record within each group Date: Wed, 24 Aug 2016 10:03:27 +0200 Lines: 42 Message-ID: <87d1ky1tm8.fsf@hf.protecting.net> References: <1471968815.2343352.703756417.6A1C9C13@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: usenet@blaine.gmane.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-Archive: encrypt X-PGP-Fingerprint: 09 4E C4 A2 B2 C5 33 1A 79 80 5D 39 BD 9B 89 39 X-Face: (1awP+uzUZkz*UdAvr%F%K`x9g3n,CWkrK[r6TS,kY~DP)$C&=IJQ;H0uPn0B$Rb>\w"Tk~9w';1@dNIad{z!y(R99X7d-uc~Vf%,i.:=~=V>.b_)hr36Jt.tF0OBe]&PB6F.(k[i^^v^8DBny^)@17gud{[!1jfZ8+ Cancel-Lock: sha1:mqoDnQ3P9OWtqvzP2rOcM9up4t0= X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) X-Pg-Spam-Score: -0.2 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org CN writes: > Hi! > > Such layout is commonly seen on real world reports where duplicated > group titles are discarded except for the first one. > > CREATE TABLE x(name TEXT,dt DATE,amount INTEGER); > > COPY x FROM stdin; > john 2016-8-20 80 > mary 2016-8-17 20 > john 2016-7-8 30 > john 2016-8-19 40 > mary 2016-8-17 30 > john 2016-7-8 50 > \. > > My desired result follows: > > john 2016-07-08 50 > 30 > 2016-08-19 40 > 2016-08-20 80 > mary 2016-08-17 20 > 30 Use window functions: SELECT CASE WHEN lag(name) OVER (PARTITION BY name ORDER BY name, dt) IS NULL THEN name ELSE NULL END, CASE WHEN lag(dt) OVER (PARTITION BY name, dt ORDER BY name, dt) IS NULL THEN dt ELSE NULL END, amount FROM x ORDER BY name, dt -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql