SELECT * FROM
(SELECT BAR.DIAGNOSES FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.DIAGNOSES IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE2 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE3 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE4 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE5 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS2 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS3 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS4 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS5 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES2 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES3 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES4 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES5 IS NOT NULL))
That result Msg 102, Level 15, State 1, Line 32 Incorrect syntax near ')'.