Ah perfect. I was struggling with CASE outside the SUM(), which was not working.
pgsql-sql by date:
Соглашаюсь с условиями обработки персональных данных