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 |
taghipour
Starting Member
5 Posts |
Posted - 2012-08-26 : 16:32:36
|
How i can get red colored records completely? i tried get it with grouping but i couldn't, cause i need all fields. i want records that have max(Date) for ID. i tried SELECT ID, MAX(Date) AS Last_DateFROM PracticingGROUP BY IDthis sql command gave me ID and Date Columns but i want Cnt and status columns tooID | Date | Cnt | Status32 | 2012-08-20 | 1 | True32 | 2012-08-21 | 2 | True32 | 2012-08-25 | 3 | True33 | 2012-08-22 | 1 | True33 | 2012-08-25 | 1 | True34 | 2012-08-20 | 1 | True34 | 2012-08-23 | 1 | false34 | 2012-08-24 | 1 | false34 | 2012-08-25 | 2 | True34 | 2012-08-26 | 3 | True |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-08-26 : 16:48:37
|
SELECT select ID,Date,Cnt,StatusFROM (select ID,Date,Cnt,Status ,rank() over (partition by ID order by date desc) as LastDate from practicing group by ID ) t1WHERE lastDate = 1JimEveryday I learn something that somebody else already knew |
 |
|
taghipour
Starting Member
5 Posts |
Posted - 2012-08-26 : 17:46:33
|
quote: Originally posted by jimf SELECT select ID,Date,Cnt,StatusFROM (select ID,Date,Cnt,Status ,rank() over (partition by ID order by date desc) as LastDate from practicing group by ID ) t1WHERE lastDate = 1JimEveryday I learn something that somebody else already knew
Thank you for help. its working but without "group by ID"best solution |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-08-26 : 17:51:57
|
you're right, I threw that group by in there since i was originally going to do justSELECT p.ID,p.Date,p.Cnt,p.StatusFROMPracticing pINNER JOIN(select ID,max(Date) as LastDate from practicinggroup by ID) t1ON p.id = t1.id and p.date = t1.lastdateJimEveryday I learn something that somebody else already knew |
 |
|
taghipour
Starting Member
5 Posts |
Posted - 2012-08-27 : 05:28:00
|
Thank you Jim you saved my time. |
 |
|
|
|
|
|
|