Thursday, April 19, 2007

SELECT MIN or MAX from multiple UNION Select Statements

My colleague was trying to select the minimum value from output from multiple Select statements that were joined by a UNION join. This MIN value has to be returned as a column as part of another complex SELECT statement.

Don't know if I explained that well but run the following code and you will understand :)

----- SELECT MIN or MAX from multiple UNION Select Statements
SELECT
ColumnList
, (SELECT
MIN(A)
FROM
(SELECT 4 AS A
UNION
SELECT 2 AS A) AS B
) AS AliasName
FROM
.....

No comments: