我的示例文件如下(四列):
ID Balance1 Balance2 Balance3
xx -1 -1 0
yy -1 0 1
每个客户都是独一无二的。我想计算负值和正值的数量,例如负数为xx
is2
和 for yy
is1
而正值(大于 0)为xx
is0
和yy
is 1
。
我可以在 Excel 中使用 COUNTIF 函数,但是如何使用 SQL 语句?
你应该试试这个:
Select ID
, Sum( Case When value < 0 Then 1 Else 0 End ) As Negatives
, Sum( Case When value > 0 Then 1 Else 0 End ) As Positive
From sample_table
Group By ID
Try This
select id,
sum (case when balance1 >0 then 1 else 0 end ) +
sum (case when balance2 >0 then 1 else 0 end ) +
sum (case when balance3 >0 then 1 else 0 end )
as positive,
sum (case when balance1 <0 then 1 else 0 end ) +
sum (case when balance2 <0 then 1 else 0 end ) +
sum (case when balance3 <0 then 1 else 0 end ) as negative
from tABLE
group by id