![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have two tables, SAFs (which contains course information) and SAF_Status (which contains records of when and how a course in the SAF table is modified). Many courses have multiple entries in the Status table. I am trying to create a query that will show all of the courses in SAF but only with the most recent status (i.e., Max(ActionDate)). The joined tables contains something like this: CourseID Subj. Number. ActionDate 123 BIOL 101 1/8/2008 123 BIOL 101 12/2/2007 120 BIOL 123 5/5/2007 231 BIOL 321 2/6/2007 120 BIOL 123 6/23/2007 What I want is: CourseID Subj. Number. ActionDate 123 BIOL 101 1/8/2008 120 BIOL 123 6/23/2007 231 BIOL 321 2/6/2007 |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
You need to add correlation to the course in the subquery to get the MAX ActionDate for the most recent status. Here is a modified query that should get you the data (I also simplified your WHERE conditions, hope got it right after removing all those parentheses): SELECT S.ActionDate, * * * * * S.Status, * * * * * C.Term, * * * * * C.Subj, * * * * * C.SubjOther, * * * * * C.Crse, * * * * * C.Course_Title , * * * * * C.CH, * * * * * C.Dept, * * * * * C.Mode, * * * * * C.Cap, * * * * * C.Site, * * * * * C.CESAFID, * * * * * C.SiteOther, * * * * * C.Inst1, * * * * * C.Inst2, * * * * * C.NewInstructor FROM dbo.SAF2_Status AS S INNER JOIN dbo.SAFs AS C * *ON S.CESAFID = C.CESAFID WHERE C.Term = 'Tvar' * *AND C.Dept = 'Dvar' * *AND S.Status IN ('DEPTSETUP', 'DEPTQUEUE', 'CERTRN', 'DEPTCORRREQ') * AND S.ActionDate = ( * * * * SELECT MAX( S1.ActionDate) * * * * FROM dbo.SAF2_Status AS S1 * * * * WHERE S1.CESAFID = C.CESAFID * * * * * * AND S1.Status IN ('DEPTSETUP', 'DEPTQUEUE', 'CERTRN', 'DEPTCORRREQ')) HTH, Plamen Ratchevhttp://www.SQLStudio.com |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Yes, it would have to be a LEFT OUTER JOIN, something like this: ... FROM dbo.SAF2_Status AS S INNER JOIN dbo.SAFs AS C * *ON S.CESAFID = C.CESAFID LEFT OUTER JOIN dbo.Instructors AS I * ON C.Inst1 = I.Inst1 ... HTH, Plamen Ratchevhttp://www.SQLStudio.com |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
One way to do this is to change the subquery that checks for the latest status, like this: ... * AND S.ActionDate = ( * * * * SELECT MAX(S1.ActionDate) * * * * FROM dbo.SAF2_Status AS S1 * * * * WHERE S1.CESAFID = C.CESAFID * * * * * *AND S1.Status IN ('DEPTSETUP', 'DEPTQUEUE', 'CERTRN', 'DEPTCORRREQ') * * * * HAVING MAX(S1.ActionDate) = (SELECT MAX(S2.ActionDate) * * * * * * * * * * * * * * * * * * * * * * * * * *FROM dbo.SAF2_Status AS S2 * * * * * * * * * * * * * * * * * * * * * * * * * *WHERE S2.CESAFID = C.CESAFID)) Note the added HAVING clause. An alternative is to change the main query FROM and WHERE, like below: ... your select <list> goes here FROM (SELECT CESAFID, * * * * * * * * * * [Status], * * * * * * * * * * MAX(ActionDate) AS ActionDate * * * FROM dbo.SAF2_Status AS S1 * * * WHERE [Status] IN ('DEPTSETUP', 'DEPTQUEUE', 'CERTRN', 'DEPTCORRREQ') * * * GROUP BY CESAFID, [Status] * * * HAVING MAX(ActionDate) = (SELECT MAX(S2.ActionDate) * * * * * * * * * * * * * * * * * * * * * * *FROM dbo.SAF2_Status AS S2 * * * * * * * * * * * * * * * * * * * * * * *WHERE S1.CESAFID = S2.CESAFID)) AS S INNER JOIN dbo.SAFs AS C * *ON S.CESAFID = C.CESAFID LEFT OUTER JOIN dbo.Instructors AS I * ON C.Inst1 = I.Inst1 WHERE C.Term = 'Tvar' * * AND C.Dept = 'Dvar' HTH, Plamen Ratchevhttp://www.SQLStudio.com |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
Is it possible to exclude courses if there most recent status is not ('DEPTSETUP', 'DEPTQUEUE', 'CERTRN', 'DEPTCORRREQ')? For instance, A course record may look like this: Course Status Date BIOL 101 DEPTSETUP 5/5/2007 BIOL 101 CERTRN 6/30/2007 BIOL 101 REMOVE 7/8/2007 Since the status REMOVE is the most recent I want the course to be excluded from the results. The SQL statement above overlooks REMOVE since it is not part of the IN clause even though it is the most recent status. |
![]() |
| Thread Tools | |
| Display Modes | |
| |