0

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'.
2
  • but with , it will gives me two columns i want only one column result, just the max sum between two columns Commented Jul 4, 2013 at 8:31
  • case when sum(bonus)>sum(Value) then sum(bonus) when sum(Value)>sum(bonus) then sum(Value) end Commented Jul 4, 2013 at 8:34

3 Answers 3

5

Your syntax is incorrect, CASE keyword goes only once:

select 
  case when sum(bonus)>sum(Value) then sum(bonus)
     else sum(Value) 
  end as MaxSum
from emp
Sign up to request clarification or add additional context in comments.

3 Comments

@chetan i checked your testing condition. It regardless of high-low, it is returning me whatever is the equal value
If they are equal it doesn't matter which one is returned. Sum(value) in this example will be returned. Change > to >= if you want Sum(bonus) for some reason.
@NenadZivkovic I i know it doesnt mattered me. No need to put >= . Thanx
3

Your case statement is wrong, try this one:

select case when sum(bonus)>sum(Value) then sum(bonus) else sum(Value) end
from emp

Comments

1

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

1 Comment

Corrected. I mistakenly thought this was for MySQL at first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.