CTE 中的自定义排序顺序

Custom Sort Order in CTE

我需要在 CTE 中获取自定义排序顺序,但错误显示

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

在 CTE 中获取自定义订单的更好方法是什么?

WITH
ctedivisiondesc
as
(
SELECT * FROM (
  SELECT --TOP 1 --[APPID]
       DH1.[ID_NUM]
      --,[SEQ_NUM_2]
      --,[CUR_DEGREE]
      --,[NON_DEGREE_SEEKING]
      ,DH1.[DIV_CDE]
      ,DDF.DEGREE_DESC 'DivisionDesc'
      --,[DEGR_CDE]
      --,[PRT_DEGR_ON_TRANSC]
      --,[ACAD_DEGR_CDE]
      ,[DTE_DEGR_CONFERRED]
      --,MAX([DTE_DEGR_CONFERRED]) AS Date_degree_conferred
      ,ROW_NUMBER() OVER (
                         PARTITION BY [ID_NUM] 
                         ORDER BY [DTE_DEGR_CONFERRED] DESC --Getting last degree
                   ) AS [ROW NUMBER]

  FROM [TmsePrd].[dbo].[DEGREE_HISTORY] As DH1
      inner join [TmsePrd].[dbo].[DEGREE_DEFINITION] AS DDF
      on DH1.[DEGR_CDE] = DDF.[DEGREE]
      --ORDER BY 
                --DIV_CDE                       Level
                --CE    Continuing Education    
                --CT    Certificate             1
                --DC    Doctor of Chiropractic  4
                --GR    Graduate                3
                --PD    Pending Division    
                --UG    Undegraduate            2

        --The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
        ORDER BY CASE 
                   WHEN DDF.DEGREE_DESC = 'Certificate' THEN 1
                   WHEN DDF.DEGREE_DESC = 'Undegraduate' THEN 2
                   WHEN DDF.DEGREE_DESC = 'Graduate' THEN 3
                   WHEN DDF.DEGREE_DESC = 'Doctor of Chiropractic' THEN 4
                   ELSE 5
                 END 

  ) AS t
  WHERE [ROW NUMBER] <= 1
)
SELECT * FROM ctedivisiondesc

您需要对外部查询进行排序。

不允许对子查询进行排序,因为它没有意义,考虑这个简单的例子:

WITH CTE AS
(    SELECT  ID
     FROM (VALUES (1), (2)) AS t (ID)
     ORDER BY ID DESC
 )
 SELECT *
 FROM CTE
 ORDER BY ID ASC;

外部查询的排序覆盖了内部查询的排序,导致浪费时间。

这不仅仅是关于外部查询的显式排序,在更复杂的场景中SQL服务器可以以任何它希望启用合并连接或分组等的方式对子查询进行排序。所以唯一的方法是保证顺序或结果是按照您的意愿对外部查询进行排序。

由于您在外部查询中可能没有所需的所有数据,因此您可能需要在 CTE 中创建另一个列以用于排序。例如

WITH ctedivisiondesc AS
(
    SELECT  *
    FROM    (   SELECT  DH1.ID_NUM,
                        DH1.DIV_CDE,
                        DDF.DEGREE_DESC AS DivisionDesc,
                        DTE_DEGR_CONFERRED,
                        ROW_NUMBER() OVER (PARTITION BY ID_NUM ORDER BY DTE_DEGR_CONFERRED DESC) AS [ROW NUMBER],
                        CASE 
                           WHEN DDF.DEGREE_DESC = 'Certificate' THEN 1
                           WHEN DDF.DEGREE_DESC = 'Undegraduate' THEN 2
                           WHEN DDF.DEGREE_DESC = 'Graduate' THEN 3
                           WHEN DDF.DEGREE_DESC = 'Doctor of Chiropractic' THEN 4
                           ELSE 5
                         END AS SortOrder
                FROM    TmsePrd.dbo.DEGREE_HISTORY AS DH1
                        INNER JOIN TmsePrd.dbo.DEGREE_DEFINITION AS DDF
                            ON DH1.DEGR_CDE = DDF.DEGREE

            ) AS t
    WHERE   t.[ROW NUMBER] <= 1
)
SELECT  ID_NUM,
        DIV_CDE,
        DivisionDesc,
        DTE_DEGR_CONFERRED
FROM    ctedivisiondesc
ORDER BY SortOrder;