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 |
|
groston
Yak Posting Veteran
61 Posts |
Posted - 2005-09-26 : 14:00:39
|
| The answer to this is probably quite easy, but I have not figured this out...Say I have a table with three columns: UserID (int), BadgeID (int) and IssueDate (datetime). Each UserID can have multiple BadgeID, and the IssueDate for each BadgeID can be different.What I am seeking is a simple query to return the most recently issued BadgeID for each ClientID. My first thought was SELECT ClientID, BadgeID, max(IssueDate)FROM TableGROUP BY ClientIDBut, this obviously doesn't work because BadgeID is not included in the GROUP BY clause. Furthermore, I really don't care to have Issuedate returned.Looking forward to your suggestions. Thanks! |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-26 : 14:27:12
|
Will this work for you?select ClientID ,max(BadgeID) as BadgeID --in case 2 different badgeIDs have the same IssueDatefrom [Table] ajoin ( select ClientID ,max(IssueDate) IssueDate from [Table] Group by ClientID ) b on a.ClientID = b.ClientIDgroup by ClientID Be One with the OptimizerTG |
 |
|
|
groston
Yak Posting Veteran
61 Posts |
Posted - 2005-09-26 : 15:27:24
|
| Thanks - was able to develop a solution based on your assistance. |
 |
|
|
|
|
|