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.
| Author |
Topic |
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2008-06-26 : 11:31:19
|
| Hey all,In a table I have two 3 columnsID / groupCode / DateDate in this might look like1 65 21/04/20082 23 21/04/20083 65 23/04/20084 23 19/04/20085 65 18/04/20086 21 21/04/2008What I want my SQL query to do is select all the Unqiue groupCodes record based on most recent date.For example3 65 23/04/20082 23 21/04/20086 21 21/04/2008Any ideas ? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-26 : 11:34:09
|
| Select t1.* from table t1 where date=(select max(date) from table where groupcode=t1.groupcode)MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-26 : 11:35:24
|
[code]SET dateformat dmyDECLARE @TABLE TABLE( ID int, groupCode int, [Date] datetime)INSERT INTO @TABLESELECT 1, 65, '21/04/2008' UNION ALLSELECT 2, 23, '21/04/2008' UNION ALLSELECT 3, 65, '23/04/2008' UNION ALLSELECT 4, 23, '19/04/2008' UNION ALLSELECT 5, 65, '18/04/2008' UNION ALLSELECT 6, 21, '21/04/2008'SELECT ID, groupCode, [Date]FROM ( SELECT ID, groupCode, [Date], row_no = row_number() OVER (PARTITION BY groupCode ORDER BY [Date] DESC) FROM @TABLE ) tWHERE t.row_no = 1/*ID groupCode Date ----------- ----------- ------------------------------------------------------ 6 21 2008-04-21 00:00:00.000 2 23 2008-04-21 00:00:00.000 3 65 2008-04-23 00:00:00.000(3 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-26 : 11:37:21
|
Not sure if OP uses SQL Server 2005 MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-26 : 11:38:31
|
we will know soon enough  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2008-06-26 : 11:50:47
|
| Hey thanks for the replies...I did forget to mention there where more columns in that table :P I triedSELECT id, dateAdded, siebelObjectId, accountNumber, accountName, fileTypeId, fileTypeName, intermediaryCode, pdfContent, completionReportDate, statusIdFROM tblPDFFilesWHERE (completionReportDate = (SELECT MAX(completionReportDate) AS Expr1 FROM tblPDFFiles AS tblPDFFiles_1 WHERE (intermediaryCode = tblPDFFiles.intermediaryCode))) and fileTypeId = 3But I only got 2 out of 3 Files cuz one record which is fileTypeId = 1 had the same intermediaryCode |
 |
|
|
Velnias
Yak Posting Veteran
58 Posts |
Posted - 2008-06-26 : 11:57:35
|
| This guy seems to work nicelySELECT id, intermediaryCode, completionReportDateFROM (SELECT id, intermediaryCode, completionReportDate, row_no = row_number() OVER (PARTITION BY intermediaryCode ORDER BY completionReportDate DESC)FROM tblPDFFilesWHERE fileTypeId = 3) tWHERE t .row_no = 1 |
 |
|
|
|
|
|
|
|