I've read a couple of questions on here that seem to answer this e.g. Return a value if no rows are found SQL
However, I'm struggling to get it to work for me. This is the output of my query as it stands:
As an example there are actually six classes, but only three display because they actually have results. I would like the classes without results to display too with null values in the No. Other column.
Here's my code so far:
DECLARE @AcademicYear varchar(9) = '2017/2018',
@Collection varchar(50) = 'Autumn';
SELECT
Test = NULLIF(COUNT(g1.Points),0),
cast(s.Year as int) as Year,
CASE r.Subject
WHEN 'English' THEN 1
WHEN 'English Language' THEN 2
WHEN 'English Literature' THEN 3
WHEN 'Maths' THEN 4
WHEN 'Science' THEN 5
WHEN 'Additional Science' THEN 6
WHEN 'Biology' THEN 7
WHEN 'Chemistry' THEN 8
WHEN 'Physics' THEN 9
WHEN 'Arabic' THEN 10
WHEN 'Dutch' THEN 11
WHEN 'French' THEN 12
WHEN 'Russian' THEN 13
WHEN 'Spanish' THEN 14
WHEN 'Urdu' THEN 15
ELSE 16
END AS SubjectSort,
r.Subject, r.Class,
0 AS GroupSort,
'SEND (' + CAST(COUNT(g1.Points) AS varchar) + ')' AS 'Group',
--Other
SUM(CASE
WHEN r.Progress in ('X','Abs','New') THEN 1
ELSE 0
END) AS 'No. Other'
FROM Results r
JOIN Grades g1
ON r.Result = g1.Grade
LEFT JOIN students s
ON r.UPN = s.UPN
WHERE r.AcademicYear = @AcademicYear
AND s.AcademicYear = @AcademicYear
AND r.Collection = @Collection
AND SEND = 'Y'
GROUP BY s.Year,
r.Subject, r.Class
Order by cast(s.year as int) desc, SubjectSort, r.Subject, r.Class, GroupSort