好的,我有一个看起来像这样的表:

ID   AMOUNT      PAID
1    50.00       Y
2    100.00      N
3    200.00      Y

我想看到类似的东西:
Total     Due Paid
350.00    1   2

所以我的SQL看起来像(在我的脑海中……它不能那样工作,这就是为什么我在这里)
select sum(amount)
,count(paid where paid='y') as due
,count(paid where paid='n') as paid
from sometable where something=somethingelse

最佳答案

select sum(amount) as total,
       sum(case paid when 'N' then 1 else 0 end) as due,
       sum(case paid when 'Y' then 1 else 0 end) as paid
from sometable where something=somethingelse

关于sql - 如何在聚合函数SQL Server中具有where子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13705532/

10-13 02:48