Given the following query:
SELECT M.year, COUNT(C.eid)
FROM Card AS C, Month AS M
WHERE EXISTS(SELECT 1 FROM Charge AS CH
WHERE CH.usingcard=C.eid AND CH.year=M.year)
GROUP BY M.year
where EXISTS is used to avoid the number of ‘Charge’ matching the given year/card changing the count. The pb is that I would like a resulting row for each possible ‘Month’ row, eg when there are no matching ‘Charge’, I would like to get 0 as count, while I currently get no row at all.
Without EXISTS, I could use an outer join. I could also probably use an UNION with a query returning 0 for case where NOT EXISTS().
Anyone has a smarter idea ?
This avoids the the problem of multiple Charge records:
This counts how may different cards were charged in the year. Non-joining rows will have
CH.usingcardof null, which won’t be counted.