将两行合并为新行并根据值求和

时间:2019-04-10 09:50:00

标签: oracle oracle11g

使用下面的查询,我得到以下格式的结果。

select count(o.orderid) as ORDER,u.usergroup as user 
from order_details o,user_deatils u
where o.ORDERCREATOR=u.USERNAME
and u.usergroup IN ('A','B','C','D')
group by u.usergroup;

订购用户 10安 20 B 30摄氏度 40天

但是我试图合并具有不同值的C和D并相应地求和它们的值

ORDER   USER
10       A
20       B
70      C+D

我尝试使用数据透视,concat,但无法获得所需格式的结果。

任何线索都是可观的:)

3 个答案:

答案 0 :(得分:0)

您可以使用CASE表达式进行汇总:

select
    case when u.usergroup = 'A' then 'A'
         when u.usergroup = 'B' then 'B'
         else 'C+D' end as user,
    count(o.orderid) as "ORDER"
from order_details o
inner join user_deatils u
    on o.ORDERCREATOR = u.USERNAME
where
    u.usergroup IN ('A','B','C','D')
group by
    case when u.usergroup = 'A' then 'A'
         when u.usergroup = 'B' then 'B'
         else 'C+D' end;

答案 1 :(得分:0)

使用decode函数:

select count(o.orderid) as ORDER,decode(u.usergroup,'C','C+D','D','C+D',u.usergroup) as user 
from order_details o,user_deatils u
where o.ORDERCREATOR=u.USERNAME
and u.usergroup IN ('A','B','C','D')
group by decode(u.usergroup,'C','C+D','D','C+D',u.usergroup);

答案 2 :(得分:0)

使用CASE和适当的联接:

IValueConverter