日期:2014-05-17  浏览次数:20426 次

数据统计问题,求解答,急急急!
表TB有4个字段A、B、C、D,实例如下

 A B C D
 1 1 2 2
 3 1 2 4
 1 1 1 1
 5 6 7 8
 9 5 8 1

------解决方案--------------------
select sum(case when A=1 then 1 else 0 end)+
 sum(case when B=1 then 1 else 0 end)+
 sum(case when C=1 then 1 else 0 end)+
 sum(case when D=1 then 1 else 0 end) as [1的次数]

from tb

------解决方案--------------------
如果不懂就用这个,这个应该非常好懂

select count(*) from
(select * from TB where A=1
union all 
select * from TB where B=1
union all 
select * from TB where C=1
union all 
select * from TB where D=1
) TT