Re: [SQL] Selecting latest value II

2001-09-27 Thread Carl van Tast
Hi, Thurstan On Thu, 20 Sep 2001 17:30:46 +0100, "Thurstan R. McDougle" <[EMAIL PROTECTED]> 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); >

Re: [SQL] Selecting latest value II

2001-09-21 Thread Thurstan R. McDougle
Hi back Carl van Tast wrote: > > Hi, Thurstan > > On Thu, 20 Sep 2001 17:30:46 +0100, "Thurstan R. McDougle" > <[EMAIL PROTECTED]> wrote: > > > [...] > >Carl van Tast had 2 good methods as follows > > > >SELECT userid, val > >FROM tbl > >WHERE NOT EXISTS (SELECT * FROM tbl AS t2 > >

Re: [SQL] Selecting latest value II

2001-09-20 Thread Thurstan R. McDougle
Look at his table structure, you will see a timestamp. His request can be rephrased as "The val field from the latest record for each userid in turn. Carl van Tast had 2 good methods as follows SELECT userid, val FROM tbl WHERE NOT EXISTS (SELECT * FROM tbl AS t2 WHERE tbl.us

Re: [SQL] Selecting latest value II

2001-09-20 Thread Chris Ruprecht
what about using 'distinct' in you select statement? - Original Message - From: "Haller Christoph" <[EMAIL PROTECTED]> To: "Patrik Kudo" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Thursday, September 20, 2001 11:02 AM Subject: Re:

Re: [SQL] Selecting latest value

2001-09-20 Thread Tom Lane
Patrik Kudo <[EMAIL PROTECTED]> writes: > 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

Re: [SQL] Selecting latest value

2001-09-20 Thread Patrik Kudo
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 DIST

Re: [SQL] Selecting latest value

2001-09-20 Thread Stephan Szabo
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 multipl

Re: [SQL] Selecting latest value II

2001-09-20 Thread Haller Christoph
What do you mean by "the latest val for each userid" I cannot understand how a value of type integer can have a attribute like "latest". Sorry, but I need at least a bit more information. Regards, Christoph > > On Thu, 20 Sep 2001, Haller Christoph wrote: > > > Try > > create NEWtable (use

Re: [SQL] Selecting latest value

2001-09-20 Thread Patrik Kudo
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 combinati

Re: [SQL] Selecting latest value

2001-09-20 Thread Haller Christoph
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); > > Thi

[SQL] Selecting latest value

2001-09-18 Thread 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, exc