pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas <maps.on@gmx.net>
To: pgsql-sql@postgresql.org
Subject: Select every first/last record of a partition?
Date: Mon, 21 May 2012 18:04:55 +0200
Message-ID: <4FBA67A7.4010302@gmx.net> (raw)

Hi,

suppose a table that has records with some ID and a timestamp.

id,    ts
3,    2012/01/03
5,    2012/01/05
7,    2012/01/07
3,    2012/02/03
3,    2012/01/05
5,    2012/03/01
7,    2012/04/04

to fetch every last row of those IDs I do:

select   id, ts
from   (   select   id, ts, row_number() over ( partition by id   order 
by ts desc ) as nr from mytab ) as x
where nr = 1


Is there a another way without a subselect?
There might be more columns so the window-functions first/last won't help.






view thread (2+ messages)  latest in thread

Message-ID: <4FBA67A7.4010302@gmx.net>
Permalink:  ../4FBA67A7.4010302@gmx.net/
Also on:    postgresql.org/message-id/4FBA67A7.4010302@gmx.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: maps.on@gmx.net
  Subject: Re: Select every first/last record of a partition?
  In-Reply-To: <4FBA67A7.4010302@gmx.net>

* 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