0

我有表:

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'.
4

3 回答 3

5

您的语法不正确,CASE关键字只出现一次:

select 
  case when sum(bonus)>sum(Value) then sum(bonus)
     else sum(Value) 
  end as MaxSum
from emp
于 2013-07-04T08:31:20.063 回答
3

你的case说法是错误的,试试这个:

select case when sum(bonus)>sum(Value) then sum(bonus) else sum(Value) end
from emp
于 2013-07-04T08:31:32.790 回答
1

其他方式:

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进行测试

于 2013-07-04T08:33:37.617 回答