Alas, you cannot do count(distinct) over
in SQL Server. You can do this with a subquery. The idea is to list the values ββin each course code (and obey other break conditions). Then just count the values ββwhere the sequence number is 1:
select sum(case when cc_seqnum = 1 then 1 else 0 end) as APE_COURSES_PER_ACADEMIC_YEAR from (select . . . , row_number () OVER (PARTITION BY s.REGISTRATION_NUMBER, APEC.APE_ID, COV.ACADEMIC_SESSION, APEC.COURSE_CODE ORDER BY (SELECT NULL) ) as cc_seqnum from . . . ) t
You have a complex request. I would suggest replacing count(distinct)
with row_number()
and make your current request a subquery or CTE for the final request.
source share