2019独角兽企业重金招聘Python工程师标准>>>
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效
当我们的查询语句有使用到统计函数和union 关键字的时候,这个问题往往就容易出现。
如何解决呢?
我们可以利用 top 100 percent来解决
在子查询中,加上 top 100 percent
伪代码示例
select * from (
select a.userid,sum(a.score) from a group by a.userid
union all
select b.userid,sum(b.score) from b group by b.userid
) as temp
改造为
select * from (
select top 100 percent a.userid,sum(a.score) from a group by a.userid
union all
select top 100 percent b.userid,sum(b.score) from b group by b.userid
) as temp