SQL Server Query Error -ORDER BY clause is invalid in views

混江龙づ霸主 提交于 2021-02-12 11:40:40

问题


I am running the below code. When I run this code, I get error message:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

I'm not sure why I am getting this error message. Help is much appreciated.

SELECT  a.DELINQ_BUCKET_GROUP, a.vv_count
FROM 
(   
    SELECT  DELINQ_BUCKET_GROUP, 
            CASE WHEN DELINQ_BUCKET_GROUP IS NULL THEN 
                    SUM(CASE WHEN DELINQ_BUCKET_GROUP IS NULL THEN 1 ELSE 0 END)
                ELSE COUNT(DELINQ_BUCKET_GROUP) 
            END AS vv_count 
    FROM    DCSReporting.dbo.DIM_DELINQUENT_BUCKET
    GROUP BY DELINQ_BUCKET_GROUP
    ORDER BY vv_count DESC
) a
WHERE rownum<=100

回答1:


If you are using SQL Server 2012 or later version, use Offset 0 Rows after Order By:

SELECT Id,
       Name       
FROM Table

ORDER BY Id 
OFFSET 0 ROWS

Hope this helps.



来源:https://stackoverflow.com/questions/36697511/sql-server-query-error-order-by-clause-is-invalid-in-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!