日期:2014-05-17 浏览次数:20613 次
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
--创建测试数据
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