all groups > sql server programming > june 2004 >
You're in the

sql server programming

group:

a simple (hopefully) sql question.


Re: a simple (hopefully) sql question. John Gilson
6/5/2004 5:55:02 PM
sql server programming:
[quoted text, click to view]

Assume table T

SELECT T.*
FROM T
INNER JOIN
(SELECT groupid, MAX(date) AS max_date
FROM T
GROUP BY groupid) AS D
ON T.groupid = D.groupid AND T.date = D.max_date

--
JAG

a simple (hopefully) sql question. tracy
6/5/2004 6:32:21 PM
hello,

QUESTION 1:
I have a table with 3 columns: ID, GroupID, Date.

eg:

ID GROUPID DATE
1 10 2004-03-02
2 10 2004-03-01
3 20 2004-03-03
4 20 2004-03-01


I want to select all records from the table (grouped by GroupID), and get
the ID of the record with the MAX Date in each group.

How can i do this?


Thanks in advance.

Re: a simple (hopefully) sql question. John Gilson
6/5/2004 6:40:34 PM
[quoted text, click to view]

SELECT MAX(id) AS id, T.groupid, T.date
FROM T
INNER JOIN
(SELECT groupid, MAX(date) AS max_date
FROM T
GROUP BY groupid) AS D
ON T.groupid = D.groupid AND T.date = D.max_date
GROUP BY T.groupid, T.date

--
JAG

[quoted text, click to view]

Re: a simple (hopefully) sql question. tracy
6/5/2004 7:28:32 PM
Ok, but what if I had same dates for the same groupID.

I would want to return the MAX(ID) in that scenario.


[quoted text, click to view]

AddThis Social Bookmark Button