一张表A(A1,A2,A3,A4,A5)
A4字段为系统分类
A5字段为手工分类
A4肯定有值
A5不一定
数据可能是这样
(A1,A2,A3,正常, )
(A1,A2,A3,正常,关注 )
(A1,A2,A3,正常, )
(A1,A2,A3,正常,次级 )
(A1,A2,A3,关注,正常 )
(A1,A2,A3,正常,正常 )判断分类的条件是如果只有系统分类有值,则以系统分类结果为准
如果系统分类和手工分类都有值,则以手工分类结果为准
现在要写个sql语句,取表中每个分类的汇总,比如分类结果正常多少条,关注多少条,次级多少条等等

解决方案 »

  1.   


    with tab as (
    select 'A1' a,'A2' b,'A3' c,'正常' A4, '' A5 from dual
    union all
    select 'A1' a,'A2' b,'A3' c,'正常' A4, '关注' A5 from dual
    union all
    select 'A1' a,'A2' b,'A3' c,'正常' A4, '' A5 from dual
    union all
    select 'A1' a,'A2' b,'A3' c,'正常' A4, '次级' A5 from dual
    union all
    select 'A1' a,'A2' b,'A3' c,'关注' A4, '正常' A5 from dual
    union all
    select 'A1' a,'A2' b,'A3' c,'正常' A4, '正常' A5 from dual
    )select count(d)
    from (select a,b,c,decode(A5,'',A4,A5) d
    from tab) t
    group by t.d
    having d = '正常'改变d的值 就能知道 各有多少了
      

  2.   

    select count(A4) from A where A4='正常' and A5 is nullselect count(A5) from A where A5='次级' and not A4 is null  and not A5 is null仅供参考
      

  3.   

    2#的可以优化一下:
    SELECT decode(A5, '', A4, A5),
           COUNT(*)
      FROM tab t
     GROUP BY decode(A5, '', A4, A5)
      

  4.   

    select decode(A5,NULL,A4,A5),COUNT(*) FROM A GROUP BY decode(A5,NULL,A4,A5);