Here are the details of the issue...
Table 1: [Student Enrollment]
| STUDENT | COURSE | ENROLL_DT |
| ------------ | ------------ | ------------ |
| 11223 | MATH123 | 01/03/2017 |
| 11223 | AUTO224 | 03/11/2017 |
| 11223 | FABR450 | 09/26/2018 |
Table 2: [Student Track]
STUDENT | TRACK | DECLARE_DT | STATUS |
---|---|---|---|
11223 | MECH-AAS | 12/04/2016 | Active |
11223 | MECH-AAS | 02/05/2016 | Discon |
11223 | ENGR-AAS | 02/20/2017 | Active |
What I'm trying to do:
Get the most recent active declared track at the time of enrollment.
| STUDENT | COURSE | ENROLL_DT | TRACK |
| ------------ | ------------ | ------------ | ------------ |
| 11223 | MATH123 | 01/03/2017 | MECH-AAS |
| 11223 | AUTO224 | 03/11/2017 | ENGR-AAS |
| 11223 | FABR450 | 09/26/2018 | ENGR-AAS |
What I've tried with no avail:
Inner join [Student Track]
to itself to get max declare date for the studentLeft join
the most recent active track at the time of enrollment to the[Student Enrollment]
table
Code:
SELECT
STUDENT, COURSE, ENROLL_DT, TRACK
FROM
[Student Enrollment] AS A
LEFT JOIN
(SELECT
STUDENT, TRACK, DECLARE_DT
FROM
[Student Track] AS B1
INNER JOIN
(SELECT STUDENT, MAX(DECLARE_DT) as MAX_DECLARE_DT
FROM [Student Track]
WHERE DECLARE_DT <= A.ENROLL_DT ***/* ENROLL_DT is out of scope!!!!!!!!!!!!!!!! =( */***
) as B2 ON A.STUDENT = B.STUDENT
WHERE
STATUS = 'Active'
3条答案
按热度按时间a0zr77ik1#
You can use an
APPLY
for this, which allows you to access values from the outer scope.Note that the join condition is moved to inside the subquery, because the subquery is (logically) exceuted once per row of the outer scope.
db<>fiddle
jchrr9hc2#
You can use
ROW_NUMBER
to join the tables.for this to work, you should have more or equal numbers in enrolment to track
You also should not use spaces in Column or table names, it must be escaped
fiddle
gcxthw6b3#
You can use SQL windonw function to get the result you want in the following steps:
Here is the query: