Skip site navigation (1) Skip section navigation (2)

Re: Selecting latest value

From: Patrik Kudo <kudo(at)partitur(dot)se>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Haller Christoph <ch(at)rodos(dot)fzk(dot)de>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Selecting latest value
Date: 2001-09-20 15:12:42
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
On Thu, 20 Sep 2001, Tom Lane wrote:

> This is what SELECT DISTINCT ON was invented for.  I don't know any
> comparably easy way to do it in standard SQL, but with DISTINCT ON
> it's not hard:
> SELECT DISTINCT ON (userid) userid, val, ts FROM table
> ORDER BY userid, ts DESC;
> See the DISTINCT ON example in the SELECT reference page for more info:


I was trying some stuff with distinct on, but in combination with max(ts),
which obviously didn't work. Now it works much better =)

/Patrik Kudo

In response to

pgsql-sql by date

Next:From: Thurstan R. McDougleDate: 2001-09-20 15:23:01
Subject: Re: table restruct...
Previous:From: Tom LaneDate: 2001-09-20 15:07:35
Subject: Re: Selecting latest value

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group