sql - historic 'slice' of table -


ms sql server.

i have table monitors status of applications school of students. students can apply multiple schools , each 1 can accept/reject/waitlist students.

table tblapplicantschools this:

applicantid    |    schoolid     |     statusid 

unique applicants can appear here applying multiple schools - 1 status per school application.

i have historic table tblapplicantschools_shadow, shadows change made on tblapplicantschools. identical above, except saves time change made, , whether insert/update/delete on original table. in shadow table, there multiple students, multiple schools multiple statuses (as move through process).

what do, slice shadow table date, , take single recent statusid of each application each school of each applicant. make sense? example:

applicantid    |    schoolid     |     statusid       |    changedate -----------------------------------------------------------------------     11                   2                 3                  22/1/2015     11                   2                 4                  30/1/2015     11                   3                 4                  25/1/2015     11                   3                 6                  29/1/2015 

so want see rows #2 , #4 above, seen single recent update of applicant #11 each school.

can give me pointer on how can done? setup little more complex, think example simplifies question clear.

thanks

if prefer ansi-92 compliant solution, or if afraid of window functions, query might useful:

select s1.applicantid, s1.schoolid, s1.statusid, s1.changedate shadow s1 inner join (     select applicantid, schoolid, max(changedate) maxdate     shadow     group applicantid, schoolid ) s2 on s1.applicantid = s2.applicantid , s1.schoolid = s2.schoolid     , s1.changedate = s2.maxdate 

Comments