dbTalk Databases Forums  

Select Min and max Date from several rows

comp.databases comp.databases


Discuss Select Min and max Date from several rows in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Select Min and max Date from several rows - 05-19-2006 , 07:58 AM






Hi,

I have a table, which stores entries of users. Each user is identified
by a unique used id. Each entry contains the userid, a timestamp and a
keyword.

I'm looking for a select statement, which will return a single row,
which contains the very first and very last entry of all.

There is no limit on the entries of user. There could be none, one, two
or many.

So if there are for example six entries entries from one user, I would
like the select statement to return:

Timestamp of First Entry, First keyword, Timestamp of Last Entry, Last
Keyword

Would this work:

SELECT first.uid, min(first.created)::TIMESTAMP, first.keyword,
min(last.created)::TIMESTAMP, last.keyword FROM userentries as first,
userentries as last WHERE first.uid = last.uid GROUP BY first.keyword,
last.keyword

Fritz


Reply With Quote
  #2  
Old   
Ed Prochak
 
Posts: n/a

Default Re: Select Min and max Date from several rows - 05-19-2006 , 12:29 PM







fritz-bayer (AT) web (DOT) de wrote:
Quote:
Hi,

I have a table, which stores entries of users. Each user is identified
by a unique used id. Each entry contains the userid, a timestamp and a
keyword.

I'm looking for a select statement, which will return a single row,
which contains the very first and very last entry of all.

There is no limit on the entries of user. There could be none, one, two
or many.

So if there are for example six entries entries from one user, I would
like the select statement to return:

Timestamp of First Entry, First keyword, Timestamp of Last Entry, Last
Keyword

Would this work:

SELECT first.uid, min(first.created)::TIMESTAMP, first.keyword,
min(last.created)::TIMESTAMP, last.keyword FROM userentries as first,
userentries as last WHERE first.uid = last.uid GROUP BY first.keyword,
last.keyword

Fritz
why not ask your database?

IOW did you try it?

It's only a select query, it isn't going to change any data, so try it
out to see if it works.
if it fails, try to think about the results it returned and why it
might have returned those values.
HTH,
ed



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.