I have the following table test
id Actual Budget CA RUB_ID
-------------------------------------------
1 20 30 201 902
1 2 330 202 902
1 220 130 207 90
1 21 30 20 12
How can get the following result
Actual Budget CA
20 130 12
I need to do sum Actual if RUB_ID =902 , sum Budget if RUB_ID =90 ,sum CA if RUB_ID =12
select
id,
case
when RUB_ID = 902 then sum(Actual) AS Actual
else case
when RUB_ID = 90
then sum(Budget) as Budget
else case
when RUB_ID = 12 then sum(CA) as CA
FROM
TEST
group by
id
The query does not return what I am looking for , how can I modify it ?
select id, sum(case when rub_id=902 then actual else 0 end) as sum_actual, sum(case when rub_id=90 then budget else 0 end) as sum_budget from test group by id