SQL Sum should be zero even if group by returns no rows¶
TIL to watch out for cases when I
- group by
- want the sum to be 0 even if there are no rows to sum
I moved the existing query to a CTE
I added a new view with one row
SELECT total_ytd_payments
FROM
(SELECT * FROM actual_sum
UNION ALL
SELECT * FROM zero_row) combined
-- ensure that the non zero row is always first
ORDER BY ABS(total) DESC
LIMIT 1
Last update:
2023-04-24