sql常见问题1:除非另外还指定了 TOP 或 FOR XML,不然,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效

除非另外还指定了 TOP 或 FOR XML,不然,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效函数

当咱们的查询语句有使用到统计函数和union 关键字的时候,这个问题每每就容易出现。 io

如何解决呢?select

咱们能够利用 top 100 percent来解决nio

在子查询中,加上 top 100 percent统计

伪代码示例查询

select * from (  top

 select a.userid,sum(a.score) from a   group by a.useridco

  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

相关文章
相关标签/搜索