[英]max sum of two sums in sql
I have table: 我有桌子:
Bonus Value
500 1400
1500 177
1800 453
1200 100
800 2500
200 50
780 740
I wanted to print the sum of column whichever is maximum. 我想打印列的总和,以最大值为准。
I Tried Following: 我尝试以下内容:
select
case when sum(bonus)>sum(Value) then sum(bonus) end
case when sum(Value)>sum(bonus) then sum(Value) end
from emp
But i had not got the result. 但是我没有得到结果。
Error: 错误:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'case'.
Your syntax is incorrect, CASE
keyword goes only once: 您的语法不正确, CASE
关键字只能使用一次:
select
case when sum(bonus)>sum(Value) then sum(bonus)
else sum(Value)
end as MaxSum
from emp
Your case
statement is wrong, try this one: 您的case
陈述有误,请尝试以下一项:
select case when sum(bonus)>sum(Value) then sum(bonus) else sum(Value) end
from emp
Another way: 其他方式:
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 ;
Test at SQL-Fiddle 在SQL-Fiddle上测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.