I have a history table for Students in SQL Server 2008.
StudentHistoryId, StudentId, Grade, CreatedAt, ModifiedAt, ModifiedBy, Active
How do I write a tsql query to get the latest modified row for all the active students ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Desi GalPosted May 16, 2011, 3:08 PM
Suthish NairPosted May 16, 2011, 3:07 PM
Select *
From Students_History SH
Where SH.Active = 1 and SH.ModifiedAt in (
select MAX(SH1.modifiedat)from Students_History SH1 where SH1.Active = 1
group by SH1.RuleId)
Desi GalPosted May 16, 2011, 2:53 PM
Select *
From Students_History SH
Where SH.Active = 1 and SH.ModifiedAt in (
select MAX(SH1.modifiedat)from Students_History SH1
group by SH1.RuleId)
Suthish NairPosted May 16, 2011, 2:48 PM
Desi GalPosted May 16, 2011, 2:32 PM
Suthish NairPosted May 16, 2011, 2:29 PM
Desi GalPosted May 16, 2011, 1:21 PM
Guest UserPosted May 14, 2011, 7:23 AM
SELECT history.* FROM history
INNER JOIN
(SELECT h.StudentId, MAX(h.ModifiedAt) AS LastModified
FROM history AS h WHERE Active = 1 GROUP BY h.StudentId) Recents
ON Recents.StudentId = history.StudentId AND Recents.LastModified = history.ModifiedAt
I haven't tested this so it may need some tweaking...
Dorababu MekaPosted May 13, 2011, 11:52 PM