Re: Selecting latest value

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Patrik Kudo <kudo(at)partitur(dot)se>
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 14:49:08
Message-ID: Pine.BSF.4.21.0109200746550.74020-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 20 Sep 2001, Patrik Kudo wrote:

> On Thu, 20 Sep 2001, Haller Christoph wrote:
>
> > Try
> > create NEWtable (userid text, val integer, ts timestamp);
> > insert into NEWtable
> > select userid, val, max(ts) from table group by userid, val;
>
> That won't work. That will give me multiple userid-val combinations. Sure,
> the userid-val combinations will be unique, but I want unique userids
> with only the latest val for each userid.

Maybe something like: (assuming that ts is unique within each userid,
otherwise what does latest mean? I haven't tried this, so it probably
doesn't parse, but...)

select userid, val from table where ts=(select max(ts) from table t where
t.userid=table.userid group by userid);

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-09-20 15:07:35 Re: Selecting latest value
Previous Message Stephan Szabo 2001-09-20 14:46:04 Re: table restruct...