数据统计问题,求解答,急急急!
表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