在oracle SQL中合并相似的值

时间:2015-03-26 10:31:02

标签: sql oracle

我有这段代码

select t_order.wh_id, t_order.bill_to_code, t_pick_detail.pick_area, count(*) as "OUC"
from t_order
INNER JOIN t_pick_detail on t_order.order_number = t_pick_detail.order_number
where t_order.wh_id = 'MP'
group by t_order.bill_to_code, t_pick_detail.pick_area, t_order.wh_id

哪个会产生这个

MP      GPS1       165
MP      GPS2       13
MP      GPS3       19
MP      BULK LIPS  128
MP      PICK FACE  19
MP             44
MP      CLS    3

现在我需要添加到代码中的是可以将3“GPSn”行合并为一个称为“GPS”的行,而不会永久更改“GPSn”,同时仍然显示所有其他数据。

1 个答案:

答案 0 :(得分:2)

您可以在caseselect

中使用order by
select o.wh_id,
       (case when o.bill_to_code like 'GPS%' then 'GPS' else o.bill_to_code
        end) as bill_to_code,
       d.pick_area, count(*) as OUC
from t_order o INNER JOIN
     t_pick_detail d
     on o.order_number = d.order_number
where o.wh_id = 'MP'
group by o.wh_id,
         (case when o.bill_to_code like 'GPS%' then 'GPS' else o.bill_to_code
          end), d.pick_area;

编辑:

您可以尝试使用此case

         (case when o.bill_to_code in ('GPS1', 'GPS2', 'GPS3') then 'GPS' else o.bill_to_code
          end)

但是,like应该有效。