title        status
aaa            1
bbb            0
ccc             1
ddd             1
SQL2000中有没有什么简单的方法统计:总记录数,status=1记录数,stauts=0记录数
得出的结果为
总记录数    正确数    错误数
    4                 3            1能不能在一条记录中实现,或是不要统计两次
如:select count(0) from table
select count(0) from table where status=1

解决方案 »

  1.   

    SELECT SUM(正确数)+SUM(错误数) AS 总记录数,SUM(正确数),SUM(错误数)
     FROM (
     SELECT COUNT(1) 正确数,0 错误数
     FROM TB
     WHERE STATUS=1
     UNION ALL 
     SELECT 0 正确数,COUNT(1) 错误数
     FROM TB
     WHERE STATUS=0)a
      

  2.   


    --创建测试数据
    if OBJECT_ID('T') is not null drop table T
    create table T(title nvarchar(20),status int)
    go
    insert into T
    select'aaa',1  union all
    select'bbb',0  union all
    select'ccc',1  union all
    select'ddd',1
    --查询
    select count(1)总记录数,sum(case when status=1 then 1 else 0 end)正确数,sum(case when status=0 then 1 else 0 end) 错误数 from T 
    /*
    总记录数        正确数         错误数
    ----------- ----------- -----------
    4           3           1(1 行受影响)
    */--删除测试数据
    drop table T
      

  3.   

    select count(1)总记录数,sum(case when status=1 then 1 else 0 end) as 正确数,sum(case when status=0 then 1 else 0 end) as 错误数 from Table 
      

  4.   

    用的是DBA_Huangzj的,解决,有点事,看完测试后忙得忘记结帖了。8——8