db2 select中使用case替代行转列操作 .

在DB2中进行行转列比较麻烦,这里我在select里用case语法代替行转列操作

 

使用以下查询语句可以实现

 

select t.CHECK_UNIT as 公司,
       t.BOE_DEPT_ID as 部门,
       sum(t.total) as 单据总数,
       sum(t.seven) as 星期天,
       sum(t.one) as 星期一,
       sum(t.two) as 星期二,
       sum(t.three) as 星期三,
       sum(t.four) as 星期四,
       sum(t.five) as 星期五,
       sum(t.six) as 星期六
  from (select bh.CHECK_UNIT,
               bh.BOE_DEPT_ID,
               count(*) as total,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 1) then
                        1
                     end) as seven,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 2) then
                        1
                     end) as one,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 3) then
                        1
                     end) as two,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 4) then
                        1
                     end) as three,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 5) then
                        1
                     end) as four,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 6) then
                        1
                     end) as five,
               count(case
                       when (DAYOFWEEK(bh.BOE_DATE) = 7) then
                        1
                     end) as six
          from sie.SIE_BOE_HEADERS bh
         where bh.BOE_DATE > TIMESTAMP('2007-7-20-12.00.00.000000')
         group by bh.CHECK_UNIT, bh.BOE_DEPT_ID, DAYOFWEEK(bh.BOE_DATE)) t
 group by t.CHECK_UNIT, t.BOE_DEPT_ID;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值