An alternative to Solution 1 (which repeats the results for each row in table #test), using the same test setup
select top 2 * from
(
select distinct(test1) as datum
from #test
union
select distinct(test2)
from #test
union
select distinct(test3)
from #test
) q
order by 1 desc
I.e. put all of the values of interest into a single query making sure there are no duplicates (distinct and union do this) then order that result and pick the top 2 entries. Results:
datum
70
50
You could use a CTE instead of the sub-query if you prefer
;WITH cte as
(
select distinct(test1) as datum
from #test
union
select distinct(test2)
from #test
union
select distinct(test3)
from #test
)
select top 2 * from cte
order by 1 desc
If you want the results in the same row but in two columns then you should use PIVOT. There is a neat trick using ROW_NUMBER which will get you the TOP 2 without using that key word:
;WITH cte as
(
select distinct(test1) as datum
from #test
union
select distinct(test2)
from #test
union
select distinct(test3)
from #test
)
select * from
(
select ROW_NUMBER() OVER (ORDER BY datum desc) AS rn, datum
from cte
) base
PIVOT (MAX(datum) FOR rn in ([1],[2])) pvt