I have a GROUP BY
/ MAX(.)
situation in SQL server. Please consider the following table, Tab1
, with yearly repeating course entries:
studentName | Course | startDate | finishDate |
---|---|---|---|
N1 | C1 | 2020-01-01 | NULL |
N1 | C1 | 2019-01-01 | 2019-02-01 |
N1 | C1 | 2018-01-01 | 2018-02-01 |
N2 | C1 | 2020-01-01 | 2020-02-01 |
N2 | C1 | 2019-01-01 | NULL |
N2 | C1 | 2018-01-01 | 2018-02-01 |
N2 | C2 | 2020-01-01 | NULL |
N2 | C2 | 2019-01-01 | 2019-02-01 |
N2 | C2 | 2018-01-01 | 2018-02-01 |
A NULL
means the student did not finish the course. I want to access the latest attempt for each student in each course. The output of
SELECT studentName, Course, MAX(startDate), MAX(finishDate)
FROM Tab1
GROUP BY studentName, Course
is:
studentName | Course | startDate | finishDate |
---|---|---|---|
N1 | C1 | 2020-01-01 | 2019-02-01 |
N2 | C1 | 2020-01-01 | 2020-02-01 |
N2 | C2 | 2020-01-01 | 2019-02-01 |
This is not correct output, as there should be NULL
for N1 <-> C1
and N2 <-> C2
combinations. How can I take MAX(finishDate)
while retaining NULL
?
Thank you.