select a.district,sum(decode(sign(a.type-3),-1,a.type,0) as "type<=3",
    sum(decode(sign(a.type-3),-1,0,a.type) as "type>3"
from actioninfo a,kfinfo b
where a.kfno = b.kfno(+)
group by a.district

解决方案 »

  1.   

    晕,group 不能用别名吧,就是不能用a.district的啊
      

  2.   

    不行的啊 ,我是oracle 8.16的
      

  3.   

    select a.district,sum(decode(sign(a.type-3),-1,a.type,0)  type<=3,
        sum(decode(sign(a.type-3),-1,0,a.type) type>3
    from actioninfo a,kfinfo b
    where a.kfno = b.kfno(+)
    group by a.district
      

  4.   

    哦,我明白了,试试这个:
    select a.district,sum(case when a.type <= 3 then 1 else 0 end) as "type<=3",
                      sum(case when a.type >  3 then 1 else 0 end) as "type>3"
    from actioninfo a,kfinfo b
    where a.kfno = b.kfno(+)
    group by a.district
      

  5.   

    不好意思,上面的错了。
    改为:
    select b.district,sum(case when a.type <= 3 then 1 else 0 end) as type<=3,
                      sum(case when a.type >  3 then 1 else 0 end) as type>3
    from actioninfo a,kfinfo b
    where b.kfno = a.kfno(+)
    group by b.district
      

  6.   

    select b.district,sum(case when a.type <= 3 then 1 else 0 end) as "type<=3",
                      sum(case when a.type >  3 then 1 else 0 end) as "type>3"
    from actioninfo a,kfinfo b
    where b.kfno = a.kfno
    group by b.district
    这个语句已经过试验!
      

  7.   

    完美的嘛就这样啦:
    select b.district,sum(case when a.type <= 3 then 1 else 0 end) as "type<=3",
                      sum(case when a.type >  3 then 1 else 0 end) as "type>3"
    from actioninfo a,kfinfo b
    where b.kfno = a.kfno(+)
    group by b.district
      

  8.   

    select district,sum(a),sum(b) from (
    select district,count(*) a,0 b from (select district,type from kfinfo left join actioninfo on kfinfo.knno=actioninfo.kfno) where type<=3 group by district
    union all
    select district,0 a,count(*) b from (select district,type from kfinfo left join actioninfo on kfinfo.knno=actioninfo.kfno) where type> 3 group by district
    ) group by district;