枢轴函数产生空值,我需要零

时间:2017-05-10 19:50:39

标签: sql oracle pivot-table oracle-apex

我正在寻找一种更有效的方法来编写此代码。问题是数据透视表产生空值,我需要零,以便我可以将列添加到一起以形成一个总列。谢谢!

    select coll_group, sub_group, collector, 
    M0,
    M1,
    M2,
    M3,
    M4,
    M5
    M6,
    M7,
    M8,
    M9,
    M10,
    M11,
    M12,
    (M1+M2+M3+M4+M5+M6+M7+M8+M9+M10+M11+M12) Total
    from(
    select coll_group, sub_group, collector,
        case when "0_M" is null then 0 else "0_M" end M0 , 
        case when "1_M" is null then 0 else "1_M" end M1 , 
        case when "2_M" is null then 0 else "2_M" end M2 , 
        case when "3_M" is null then 0 else "3_M" end M3 , 
        case when "4_M" is null then 0 else "4_M" end M4 ,
        case when "5_M" is null then 0 else "5_M" end M5 , 
        case when "6_M" is null then 0 else "6_M" end M6 , 
        case when "7_M" is null then 0 else "7_M" end M7 ,
        case when "8_M" is null then 0 else "8_M" end M8 , 
        case when "9_M" is null then 0 else "9_M" end M9 , 
        case when "10_M" is null then 0 else "10_M" end M10 , 
        case when "11_M" is null then 0 else "11_M" end M11 , 
        case when "12_M" is null then 0 else "12_M" end M12                
        from
    (select * from (
      select coll_group, sub_group, collector, low_activity_days,
        months_between(trunc(sysdate, 'MM'), month) as month_offset
      from low_activity_days_collect_t
      where month >= add_months(trunc(sysdate, 'MM'), -13)
      and month < trunc(sysdate, 'MM')
    ) src
    pivot (sum(low_activity_days) as M
      for month_offset in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))))

2 个答案:

答案 0 :(得分:0)

使用NVL(0_M,0) M0代替case when "0_M" is null then 0 else "0_M" end M0等等......

答案 1 :(得分:0)

据我所知,null和1的总和是1,所以为什么要担心呢?