好的,我有一个看起来像这样的表:
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
答案 0 :(得分:5)
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
答案 1 :(得分:1)
还有一个选择
SELECT SUM(AMOUNT) AS Total,
COUNT(CASE WHEN PAID = 'Y' THEN PAID END) AS Paid,
COUNT(CASE WHEN PAID = 'N' THEN PAID END) AS Due
FROM sometable
WHERE something = somethingelse