gpt4 book ai didi

sql - 如何在聚合函数 SQL Server 中有一个 where 子句

转载 作者:行者123 更新时间:2023-12-02 08:44:19 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com