1、对union all中的每个查询分别排序
现在有A表的数据和B表的数据,现需要将两个表的数据按照一定的条件进行查询之后,将两个结果集并起来,而且查询之后的数据是不会混在一起的,结果集先是A表后是B表。
另外,如果对这两个表的查询数据的排序条件不一样,需要分别排序,那么相关代码如下:
1)如果这种写法就会报错:
select a,b from tablea where … order by a
union all
select a,b from tableb where … order by b,c
2)试着改成下面的两种写法,就不会报错了:
第一种:
select * from(select a,b from tablea where … order by a)m
union all
select * from(select a,b from tableb where … order by b,c)n
第二种:
select * from(select TOP 100 Percent a,b from tablea where … order by a)m
union all
select * from(select TOP 100 Percent a,b from tableb where … order by b,c)n