Suppose there is data like below:
ID Name Cost
ID1 A 10
ID1 A 60
ID1 B 20
ID1 C 20
ID2 B 10
ID2 B 50
ID2 C 50
ID3 B 5
Here in the table above, ID and NAME are not unique. And I want to get SUM values based on NAME, so the expected result is like below:
ID A_Costs B_Costs C_Costs AB_Costs
ID1 70 20 20 90
ID2 60 50 60
ID3 5 5
A_Cost, B_Costs, and C_Costs are costs when the name is A, B or C. But what do I do if I want to get costs when the name is A and B? So what I was trying to do was this:
Select t2.ID,
SUM(DECODE (t2.name, 'A', t2.Cost, null)),
SUM(DECODE (t2.name, 'B', t2.Cost, null))
--(select sum(t1.cost) from table t1. where t1.name in ('A','B') and t1.id = t2.id)
from table t2
group by t2.id
But this does not work. How do I get the costs when the name is A and B like the line I commented out? Is there any effective way to get the value like that in one query?
Thank you in advance.