How do you find the greatest of three numbers in using CASE statment in sql?

Assuming you want to find the greater of 3 values that are in a table, you could do it this way:

CREATE TABLE #T

(

Column1 INT,

Column2 INT,

Column3 INT

)

INSERT #T VALUES (-100, 25, 1000)

INSERT #T VALUES (2, 66, 10)

INSERT #T VALUES (110, 0, 200)

INSERT #T VALUES (-1, -2, -3)

INSERT #T VALUES (1, 2, 3)

INSERT #T VALUES (3, 2, 1)

INSERT #T VALUES (1, 3, 2)

INSERT #T VALUES (2, 3, 3)

SELECT CASE WHEN Column1 >= Column2 AND Column1 >= Column3 THEN Column1

WHEN Column2 >= Column3 AND Column2 >= Column1 THEN Column2

ELSE Column3

END AS MaxValue

FROM #T

DROP TABLE #T

Improve Answer Discuss the question "How do you find the greatest of three numbers in using CASE statment in sql?" Watch Question

First answer by Eisaacs. Last edit by Eisaacs. Contributor trust: 47 [recommend contributor]. Question popularity: 0 [recommend question]

Research your answer:

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Databases > How do you find the greatest of three numbers in using CASE statment in sql?