In this post (Adding a Query to a Subquery then produces no results) @D-Shih provided a great solution, which I would like to extend.
How do I add to the results returned, the count of reports by that teacher, even if the subquery is only finding the last one?
I'm trying to solve the <???> AS CountOfReports,
line below, but my SQL skills are not that great.
SELECT
t.NAME,
t1.REPORTINGTYPE,
<???> AS CountOfReports, <<<< ****
t1.REPORTINGPERIOD
FROM
teachers AS t
INNER JOIN
(SELECT
*,
(SELECT COUNT(*) FROM REPORTS tt
WHERE tt.TEACHER_ID = t1.TEACHER_ID
AND tt.REPORTINGPERIOD >= t1.REPORTINGPERIOD) rn
FROM
REPORTS t1) AS t1 ON t1.TEACHER_ID = t.id AND rn = 1
ORDER BY
t.NAME