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

Re: Selecting latest value

From: Patrik Kudo <kudo(at)partitur(dot)se>
To: Haller Christoph <ch(at)rodos(dot)fzk(dot)de>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Selecting latest value
Date: 2001-09-20 13:57:42
Message-ID: Pine.BSF.4.31.0109201555480.17720-100000@tb303.partitur.se (view raw or flat)
Thread:
Lists: pgsql-sql
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.

/Patrik Kudo

> >
> > 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

pgsql-sql by date

Next:From: David M. RichterDate: 2001-09-20 14:31:20
Subject: Re: table restruct...
Previous:From: Kovacs BaldvinDate: 2001-09-20 13:40:02
Subject: Re: table restruct...

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