数据表 格式如下:
a1 a2  a3
1  a    1
1  a    1
2  f    1
2  d    1
3  a    
4  e    
4  d    
5  e    
5  g    
6  e    我想按照 a1 字段分组  统计a3字段 为空的 和不为空的字段
同时统计出来,用一条语句
 多谢各位大侠了,本人菜鸟

解决方案 »

  1.   


    select a1,sum(case when a3 is null then 1 else 0 end) as a3_num1 --a3字段为空的
            ,sum(case when a3 is not null then 1 else 0 end) as a3_num2 --a3字段不为空的
    from table
    group by a1
      

  2.   


    select a1,
           count(decode(a3,null,1,null)) as cnt1,  --为空的
           count(decode(a3,null,null,1)) as cnt2   --不为空的
      from table_name
     group by a1;
      

  3.   

    select a1,sum(nvl(a3,0)) from table_name group by a1
      

  4.   

      select a1,
                  count(decode(a3,null,1,null)) as num1,  --a3 为空
                  sum(nvl(a3,0)) as num2                  --a3 为空
                   from table_name group by a1