除非另外還指定了 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