How to get TOP 1 value from UNION from 2 columns?

This is a query, how to select the top from the result? This is for SQL Server.

SELECT column1 
FROM table 
WHERE column2 = 'Whatever' AND column3 = 'Sure'

UNION

SELECT column4 
FROM table 
WHERE column2 = 'Whatever' AND column3 = 'Sure'
+3
source share
3 answers

Try the following:

SELECT TOP 1 * FROM
(
  SELECT column1 FROM table WHERE column2 = 'Whatever' AND column3 = 'Sure'
  UNION
  SELECT column4 FROM table WHERE column2 = 'Whatever' AND column3 = 'Sure'
) R
ORDER BY Column1
+7
source

Try:

SELECT max(Column) FROM (
  SELECT column1 as Column FROM table WHERE ....
  UNION 
  SELECT column4 as Column FROM table WHERE ....
)
+2
source

You mean

SELECT TOP 1 * 
FROM (
    SELECT column1 FROM table WHERE column2 = 'Whatever' AND column3 = 'Sure'
    UNION
    SELECT column4 FROM table WHERE column2 = 'Whatever' AND column3 = 'Sure')
+1
source

All Articles