agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Harald Fuchs <hari.fuchs@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Display group title only at the first record within each group
Date: Wed, 24 Aug 2016 10:03:27 +0200
Message-ID: <87d1ky1tm8.fsf@hf.protecting.net> (raw)
References: <1471968815.2343352.703756417.6A1C9C13@webmail.messagingengine.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
CN <cnliou9@fastmail.fm> 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
view thread (2+ messages)
Message-ID: <87d1ky1tm8.fsf@hf.protecting.net>
Permalink: ../87d1ky1tm8.fsf@hf.protecting.net/
Also on: postgresql.org/message-id/87d1ky1tm8.fsf@hf.protecting.net
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: hari.fuchs@gmail.com
Subject: Re: Display group title only at the first record within each group
In-Reply-To: <87d1ky1tm8.fsf@hf.protecting.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox