Oracle 19c
I have a table with more than 20 columns with numeric values and I want to sum them by custom groups based in one column. For example in the next table, I want the sum of Quant_1
and Quant_2
columns, based on groups G1, G2 and G3 where G1 are the rows where Category
(numeric values) is <= 2, G2 are the rows where Category
is <= 4 even 1 and 2, and G3 are the rows where Category
is <= 6.
Name | Category | Quant_1 | Quant_2 |
---|---|---|---|
name1 | 1 | 10 | 5 |
name1 | 1 | 10 | 5 |
name2 | 2 | 11 | 6 |
name1 | 3 | 12 | 7 |
name2 | 4 | 13 | 8 |
name1 | 5 | 14 | 9 |
name2 | 6 | 15 | 10 |
name1 | 7 | 20 | 20 |
So, in this example I want to do the aggregation by Name
and my custom group to get this result:
Name | Group | Quant_1 | Quant_2 |
---|---|---|---|
name1 | G1 | 20 | 10 |
name1 | G2 | 32 | 17 |
name1 | G3 | 46 | 26 |
name2 | G1 | 11 | 6 |
name2 | G2 | 24 | 14 |
name2 | G3 | 39 | 24 |
How do I get those column sums based on these conditions?
Thanks in advance for your help.