1

I have below mock up data,

itemNum    itemName       type        count  
-------------------------------------------------  
1111       A001           1           2  
1111       A001           1           4  
1111       A001           3           2  
1111       A001           3           5  
1111       A001           3           3  
1111       A001           3           6  
2222       A002           1           3  
2222       A002           1           5  
2222       A002           2           4  
2222       A002           2           7  
2222       A002           3           8  
2222       A002           3           9  

i need the sums of one item per type listed in the same row,(there will be only 3 types, so will have three columns sum1, sum2, sum3.) The result i need is ,

itemNum    itemName      sum1      sum2      sum3  
--------------------------------------------------  
1111        A001         6          7         9
2222        A002         8          11        17

How do i write the oracle sql script ? Thanks.

Benson
  • 189
  • 2
  • 9

2 Answers2

0

Just use conditional aggregation:

select itemnum, itemname,
       sum(case when type = 1 then count end) as sum_1,
       sum(case when type = 2 then count end) as sum_2,
       sum(case when type = 3 then count end) as sum_4
from t
group by itemnum, itemname;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You can use pivot as following:

Select * from
(Select * from your_table)
Pivot
(
Sum(count) 
for type in (1 as sum_1, 2 as sum_2, 3 as sum_3)
)

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31