Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 trouble with select and possible group by

Author  Topic 

kaisdd
Starting Member

17 Posts

Posted - 2007-08-31 : 10:29:38
hey there, i´m looking for a little help with a query.

i got the following table structure and data:
CommentID, CommentPhotoID, CommentDate, CommentText
1, 1, 2007-05-25, 'Comment A'
2, 2, 2007-05-26, 'Comment B'
3, 1, 2007-05-27, 'Comment C'
4, 1, 2007-05-28, 'Comment D'

what i like to get is a list of new comments ordered by CommentDate desc but having each photo only once. in the end, the list should look like:

4, 1, 2007-05-28, 'Comment D'
2, 2, 2007-05-26, 'Comment B'

tips? anyone?
thank you very much!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-31 : 10:32:14
SELECT t1.CommentID, t1.CommentPhotoID, t1.CommentDate, t1.CommentText
FROM Table1 AS t1
INNER JOIN (
SELECT CommentPhotoID, MAX(CommentDate) AS maxDate FROM Table1 GROUP BY CommentPhotoID
) AS d ON d.CommentPhotoID = t1.CommentPhotoID AND d.maxDate = t1.CommentDate
ORDER BY t1.CommentPhotoID


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

kaisdd
Starting Member

17 Posts

Posted - 2007-08-31 : 11:14:16
wow! that was fast and it works fantastic. thank you very much! kai
Go to Top of Page
   

- Advertisement -