Some times we have to group our data on a specific condition for example in the following table
Create table scenario (studentID smallint, certID smallint, issuedate datetime)GO
populated with data asinsert into scenario --values (studentID, certID, issueDate)select 1,12,'2008-10-13' union all select 1,13,'2009-06-11' union all select 2,16,'2009-08-10' union all select 2,21,'2007-07-23' union all select 3,23,'2009-11-25' GO
I can have a requriement like
"to get records grouped on studentID with last certificate issued according to issue date."
So we can use group by and get required result using the following query
select a.studentID,a.certID,a.issuedatefrom scenario a,(select studentID,max(issuedate)issuedatefrom scenario group by studentID)as bwhere a.studentID = b.studentIDand a.issuedate = b.issuedateorder by a.studentID
No comments:
Post a Comment