对比下面3次查询,第一次会报错,第二次把整体查询结果按照dealTime 倒序,如果想让union all的两个查询中都先按照指定排序取前N跳然后再union all则参考第三种方式
select top 10 * FROM table_a WITH ( NOLOCK ) where specId=25893 order by dealTime desc
union all
select top 10 * FROM table_b WITH ( NOLOCK ) where specId=25894 order by dealTime desc
-------------------------------------------------------------------------
select top 10 * FROM table_a WITH ( NOLOCK ) where specId=25893
union all
select top 10 * FROM table_b WITH ( NOLOCK ) where specId=25894 order by dealTime desc
---------------------------------------------------------------------
select * from (select top 10 * FROM table_a WITH ( NOLOCK ) where specId=25893 order by dealTime desc) as t1
union all
select * from (select top 10 * FROM table_b WITH ( NOLOCK ) where specId=25894 order by dealTime desc) as t2