Skip to content
Advertisement

SQL group and condition sum in the last row

I have several items which belongs to different group, some are unknown group. I need the sum of each group by date. I just try below SQL but still not exactly what I want:

DB: SAP HANA

Data:

Item PG NET_VALUE_USD EMANAGER_DATE
1 1 100 2021-09
2 1 200 2021-10
3 2 300 2021-09
4 2 400 2021-09
5 3 500 2021-09
6 3 200 2021-11
7 3 300 2021-11
8 900 2021-09

Expected result:

PG 2021-09 2021-10 2021-11
? 900 0 0
1 100 200 0
2 700 0 0
3 500 0 500
total 2200 200 500

Advertisement

Answer

You can adjust your query and use GROUPING SETS:

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