[英]max sum of two sums in sql
我有桌子:
Bonus Value
500 1400
1500 177
1800 453
1200 100
800 2500
200 50
780 740
我想打印列的總和,以最大值為准。
我嘗試以下內容:
select
case when sum(bonus)>sum(Value) then sum(bonus) end
case when sum(Value)>sum(bonus) then sum(Value) end
from emp
但是我沒有得到結果。
錯誤:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'case'.
您的語法不正確, CASE
關鍵字只能使用一次:
select
case when sum(bonus)>sum(Value) then sum(bonus)
else sum(Value)
end as MaxSum
from emp
您的case
陳述有誤,請嘗試以下一項:
select case when sum(bonus)>sum(Value) then sum(bonus) else sum(Value) end
from emp
其他方式:
SELECT TOP (1) *
FROM
( SELECT SUM(Bonus) AS MaxSum, 'Bonus' AS SummedColumn FROM emp
UNION
SELECT SUM(Value), 'Value' FROM emp
) AS tmp
ORDER BY MaxSum DESC ;
在SQL-Fiddle上測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.