I have a data set containing a list of exams, the qualifications/units they're associated with, and whether the exam was passed or failed. The data looks something like this:
candidate | qualification | unit | exam | exam_status
-----------------------------------------------------
C1 | Q1 | U1 | E1 | Passed
C1 | Q1 | U2 | E2 | NULL
C1 | Q1 | U2 | E3 | Passed
C1 | Q1 | U3 | E4 | Passed
C1 | Q1 | U3 | E5 | Passed
From this I need to be able to calculate both the total number of units that exist for each qualification, as well as how many of those units have been passed by the candidate.
In theory, each unit should have one exam (although if the candidate fails the exam the first time there may be multiple records) so I should be able to get the data I need using the following query:
select
candidate,
qualification,
count(distinct unit),
count(
case when exam_status = 'Passed' then 1 else null end
)
from example_table
group by candidate, qualification
However, for whatever reason, some candidates have passed the same exam multiple times, meaning that my count of units passed is sometimes more than the total number of units.
I would like to do something like:
count(distinct exam case when exam_status = 'Passed' then 1 else null end)
to only select unique exams that have been passed, but that fails.
Does anyone know how i can achieve this? Thanks in advance.