Skip to content
Advertisement

How calculate filtered by date aggregate sum after grouping by some column in SQL?

I have a table which contains payments received by person per date. I.e.

We know how to calculate aggregate sum in SQL:

We know how to calculate aggregated sum for some period of dates:

Here is the question: what if I need last aggregate but I dont want to remove persons which never had payments on 12-12-2020? In other words, after grouping and summing I want to see something like that

Probably I can achieve this with join, but this looks like an overhead. Any options?

Advertisement

Answer

You can try to use operator CASE:

If you need to get several dates then use predicate IN:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement