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: Pine.BSF.4.31.0109201711020.17720-100000@tb303.partitur.se (view raw or flat)
Thread:
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:
> http://www.ca.postgresql.org/users-lounge/docs/7.1/postgres/sql-select.html

Thanks!

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-2014 The PostgreSQL Global Development Group