select colx,
sum(case when col=bm1 then 1 else 0 end) as bm1
sum(case when  col=bm2 then 1 else 0 end) as bm2
from table
group by colX

解决方案 »

  1.   

    更正select colx,
    sum(case when col=bm1 then 1 else 0 end) as bm1
    sum(case when  col=bm2 then 1 else 0 end) as bm2
    from table
    group by colX,col
      

  2.   

    CrazyFor:3ks 
     see again, different recorderset different result column nums
      

  3.   

    It's just as crosstab in PB
      

  4.   

    仅有bm1和bm2吗?有没有其他不确定的?
      

  5.   

    Yun~~~~~~~~~~~~~~!
    Just Like Test Our E Level!!UP!
      

  6.   

    SELECT sp,SUM(CASE WHEN bm='bm1' THEN bb ELSE NULL) as bm1,
              SUM(CASE WHEN bm='bm2' THEN bb ELSE NULL) as bm2
    FROM(SELECT  sp,bm,SUM(s1) as bb
         from tab
         group by sp,bm
         ) tmp
    GROUP BY sp--想不出有其它情況(不止bm1,bm2)的方法。
      

  7.   

    to mousean:
    this problem from 
    http://expert.csdn.net/Expert/topic/1229/1229844.xml?temp=9.800357E-02
      

  8.   

    select colx,
    (case when (select count(*) from table where col='bm1')>0 then (sum(case when (select count(*) from table where col='bm2')>0 then ((case when col=bm1 then 1 else 0 end) as bm1) end))
    sum(case when  col=bm2 then 1 else 0 end) as bm2
    from table
    group by colX
      

  9.   

    select colx,
    (case
    when (select count(*) from table where col='bm1')>0 then 
           (sum(case when col=bm1 then 1 else 0 end) as bm1)
    end)(case
    when (select count(*) from table where col='bm2')>0 then 
           (sum(case when col=bm2 then 1 else 0 end) as bm2)
    end)
    from table
    group by colX