0

我的示例文件如下(四列):

ID Balance1 Balance2 Balance3
xx    -1       -1       0
yy    -1        0       1

每个客户都是独一无二的。我想计算负值和正值的数量,例如负数为xxis2和 for yyis1而正值(大于 0)为xxis0yyis 1

我可以在 Excel 中使用 COUNTIF 函数,但是如何使用 SQL 语句?

4

2 回答 2

1

你应该试试这个:

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
于 2013-11-12T07:46:11.050 回答
0
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
于 2013-11-12T08:00:18.723 回答