You can't use two different order by
clauses when joining tables with a union
. (See this Stackoverflow) So why does it work when I use a CTE?
;with x as (
select top 1 * from myTable order by col1
union select top 1 * from myTable order by col2
)
select *
from x
The query within the CTE does not work standalone.