问一个关于count的比较难的问题
比如有个表T1
a b c
3 2 春
4 3 夏
0 9 春
7 4 冬
0 3 秋
1 0 春
select count(a),count(b) from T1 group by c
==============================
我想求出的是a不等与0的个数,和b不等于0的个数,请问这样要什么实现。
------解决方案--------------------select sum(case when a <> 0 then 1 else 0 end),sum(case when b <> 0 then 1 else 0 end) from T1 group by c
------解决方案--------------------select sum(case a when 0 then 0 else 1 end),
sum(case b when 0 then 0 else 1 end),
c
from T1
group by c
------解决方案--------------------Select
SUM(Case When a != 0 Then 1 Else 0 End) As Counta,
SUM(Case When b != 0 Then 1 Else 0 End) As Countb
From T1
------解决方案--------------------Create Table T1
(a Int,
b Int,
c Nvarchar(10))
Insert T1 Select 3, 2, N '春 '
Union All Select 4, 3, N '夏 '
Union All Select 0, 9, N '春 '
Union All Select 7, 4, N '冬 '
Union All Select 0, 3, N '秋 '
Union All Select 1, 0, N '春 '
GO
Select
SUM(Case When a != 0 Then 1 Else 0 End) As Counta,
SUM(Case When b != 0 Then 1 Else 0 End) As Countb
From T1
GO
Drop Table T1
--Result
/*
Counta Countb
4 5
*/