Re: Selecting latest value II

From: Carl van Tast <vanTast(at)Pivot(dot)at>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting latest value II
Date: 2001-09-20 19:47:11
Message-ID: 90hkqtoul1cnb9pqpn47i7544j9oik9s3d@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, Thurstan

On Thu, 20 Sep 2001 17:30:46 +0100, "Thurstan R. McDougle"
<trmcdougle(at)my-deja(dot)com> wrote:

> [...]
>Carl van Tast had 2 good methods as follows
>
>SELECT userid, val
>FROM tbl
>WHERE NOT EXISTS (SELECT * FROM tbl AS t2
> WHERE tbl.userid=t2.userid AND t2.ts > tbl.ts);
>
>or
>
>SELECT tbl.userid, tbl.val
>FROM tbl
> INNER JOIN
> (SELECT userid, max(ts) AS maxts
> FROM tbl
> GROUP BY userid) AS t2
> ON (tbl.userid=t2.userid AND tbl.ts=t2.maxts);

... although I like Tom Lane's even better. This one should outperform
all others, especially my first one:

> SELECT DISTINCT ON (userid) userid, val, ts FROM table
> ORDER BY userid, ts DESC;

That's the reason I'm here: learning by helping :-)

Kind regards
Carl van Tast

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2001-09-20 19:50:02 Re: Out of free buffers... HELP!
Previous Message Diehl, Jeffrey 2001-09-20 19:06:27 Re: Out of free buffers... HELP!