select top 1
isnull((select count(*) from test),0) as 记录总数,
isnull((select count(*) from test where status='a'),0) as a的记录数量,
isnull((select count(*) from test where status='b'),0) as b的记录数量,
isnull((select count(*) from test where status='c'),0) as c的记录数量
from test

解决方案 »

  1.   

    select sum(1) as total, sum((case status when 'a' then 1 else 0 )) as a,
    sum((case status when 'b' then 1 else 0 )) as b,sum((case status when 'a' then c else 0 )) as c form table
      

  2.   

    对于一般情况可以,但如果是还有一字段sid,要求group by sid(即根据每一个sid来统计status为a、b、c的记录数),那又该如何解决?
      

  3.   

    select sid, sum(1) as total, sum((case status when 'a' then 1 else 0 )) as a,
    sum((case status when 'b' then 1 else 0 )) as b,sum((case status when 'a' then c else 0 )) as c form table
    group by sid
      

  4.   

    提示语法错误,line1,near‘)’
      

  5.   

    TRY:
    select sid, sum(1) as total, sum((case status when 'a' then 1 else 0 end)) as a,
    sum((case status when 'b' then 1 else 0 end)) as b,sum((case status when 'a' then c else 0 end)) as c form table
    group by sid
      

  6.   

    select count(*) as total, 
    sum((case status when 'a' then 1 else 0 )) as a,
    sum((case status when 'b' then 1 else 0 )) as b,
    sum((case status when 'c' then 1 else 0 )) as c 
    form table
      

  7.   

    ok啦,多谢 CrazyFor(蚂蚁) 、zqllyh(找感觉)