Re: Selecting latest value

From: Haller Christoph <ch(at)rodos(dot)fzk(dot)de>
To: kudo(at)partitur(dot)se (Patrik Kudo)
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting latest value
Date: 2001-09-20 11:11:14
Message-ID: 200109200911.LAA25095@rodos
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Try
create NEWtable (userid text, val integer, ts timestamp);
insert into NEWtable
select userid, val, max(ts) from table group by userid, val;

Regards, Christoph
>
> Hi,
>
> I have a table which basically looks like this:
>
> create table (userid text, val integer, ts timestamp);
>
> This table holds multiple values for users, timestamped for history
> reasons.
>
> Now I need to fetch the latest val for each userid to insert into a new
> table (with about the same schema, except for uniqueness on userid).
> I belive this should be a trivial task, but I'm experience total lack of
> insight here...
>
> Comments?
>
> /Patrik Kudo
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Kovacs Baldvin 2001-09-20 13:40:02 Re: table restruct...
Previous Message Andreas Joseph Krogh 2001-09-20 10:56:33 Re: Trigger Problem