SQL Server:ORDER BY子句在视图,内联函数,派生表,子查询中无效

时间:2017-01-07 07:09:35

标签: sql-server

我在存储过程中有这个查询):

SELECT *
FROM   
    dbo.JointHistory c
OUTER APPLY
    (SELECT 
         MAX(CASE NdtType WHEN 'RT' THEN RequestNumber END) AS MasterRTRequestNumber,
         MAX(CASE NdtType WHEN 'RT' THEN NdtReportNumber END) AS ContractorRTRequestNumber,
         MAX(CASE NdtType WHEN 'RT' THEN ResponseReportNumber END) AS ContractorRTReportNumber,
         MAX(CASE NdtType WHEN 'RT' THEN ResponseReportDatetime END) AS RTDate,
         MAX(CASE NdtType WHEN 'RT' THEN Defect END) AS RTDefect,
         MAX(CASE NdtType WHEN 'RT' THEN Remark END) AS RTSegment,
         MAX(CASE NdtType WHEN 'PT' THEN RequestNumber END) AS MasterPTRequestNumber,
         MAX(CASE NdtType WHEN 'PT' THEN NdtReportNumber END) AS ContractorPTRequestNumber,
         MAX(CASE NdtType WHEN 'PT' THEN ResponseReportNumber END) AS ContractorPTReportNumber,
         MAX(CASE NdtType WHEN 'PT' THEN ResponseReportDatetime END) AS PTDate,
         MAX(CASE NdtType WHEN 'PT' THEN Defect END) AS PTDefect,
         MAX(CASE NdtType WHEN 'PT' THEN Remark END) AS PTSegment,
         MAX(CASE NdtType WHEN 'PWHT' THEN RequestNumber END) AS MasterPWHTRequestNumber,
         MAX(CASE NdtType WHEN 'PWHT' THEN NdtReportNumber END) AS ContractorPWHTRequestNumber,
         MAX(CASE NdtType WHEN 'PWHT' THEN ResponseReportNumber END) AS ContractorPWHTReportNumber,
         MAX(CASE NdtType WHEN 'PWHT' THEN ResponseReportDatetime END) AS PWHTDate,
         MAX(CASE NdtType WHEN 'PWHT' THEN Defect END) AS PWHTDefect,
         MAX(CASE NdtType WHEN 'PWHT' THEN Remark END) AS PWHTSegment,
         MAX(CASE NdtType WHEN 'MT' THEN RequestNumber END) AS MasterMTRequestNumber,
         MAX(CASE NdtType WHEN 'MT' THEN NdtReportNumber END) AS ContractorMTRequestNumber,
         MAX(CASE NdtType WHEN 'MT' THEN ResponseReportNumber END) AS ContractorMTReportNumber,
         MAX(CASE NdtType WHEN 'MT' THEN ResponseReportDatetime END) AS MTDate,
         MAX(CASE NdtType WHEN 'MT' THEN Defect END) AS MTDefect,
         MAX(CASE NdtType WHEN 'MT' THEN Remark END) AS MTSegment,
         MAX(CASE NdtType WHEN 'UT' THEN RequestNumber END) AS MasterUTRequestNumber,
         MAX(CASE NdtType WHEN 'UT' THEN NdtReportNumber END) AS ContractorUTRequestNumber,
         MAX(CASE NdtType WHEN 'UT' THEN ResponseReportNumber END) AS ContractorUTReportNumber,
         MAX(CASE NdtType WHEN 'UT' THEN ResponseReportDatetime END) AS UTDate,
         MAX(CASE NdtType WHEN 'UT' THEN Defect END) AS UTDefect,
         MAX(CASE NdtType WHEN 'UT' THEN Remark END) AS UTSegment
     FROM   
         (SELECT TOP 1 WITH TIES 
              NRD.NdtType, NRD.RequestNumber, NR.NdtReportNumber,
              NRD.ResponseReportDatetime, NRD.Defect,
              NRD.ResponseReportNumber, NRD.Remark
          FROM   
              dbo.NdtReportDetails NRD
          LEFT OUTER JOIN 
              NdtReports NR ON NRD.ReportId = NR.Id
          WHERE  
              NRD.JointId = c.Id
              AND NRD.NdtType IN ( 'RT', 'PT', 'PWHT', 'MT', 'UT' )
          ORDER BY 
              NRD.Id DESC) i) b

但是基于此问题:https://stackoverflow.com/questions/41518618/select-top-ties-in-sql-cant-return-expected-data/41518699

,查询结束时遇到了问题

所以我的查询改为:

 SELECT *
FROM   dbo.JointHistory c
       OUTER Apply (SELECT Max(CASE NdtType WHEN 'RT' THEN RequestNumber END) AS MasterRTRequestNumber,
                           Max(CASE NdtType WHEN 'RT' THEN NdtReportNumber END) AS ContractorRTRequestNumber,
                           Max(CASE NdtType WHEN 'RT' THEN ResponseReportNumber END) AS ContractorRTReportNumber,
                           Max(CASE NdtType WHEN 'RT' THEN ResponseReportDatetime END) AS RTDate,
                           Max(CASE NdtType WHEN 'RT' THEN Defect END) AS RTDefect,
                           Max(CASE NdtType WHEN 'RT' THEN Remark END) AS RTSegment,

                           Max(CASE NdtType WHEN 'PT' THEN RequestNumber END) AS MasterPTRequestNumber,
                           Max(CASE NdtType WHEN 'PT' THEN NdtReportNumber END) AS ContractorPTRequestNumber,
                           Max(CASE NdtType WHEN 'PT' THEN ResponseReportNumber END) AS ContractorPTReportNumber,
                           Max(CASE NdtType WHEN 'PT' THEN ResponseReportDatetime END) AS PTDate,
                           Max(CASE NdtType WHEN 'PT' THEN Defect END) AS PTDefect,
                           Max(CASE NdtType WHEN 'PT' THEN Remark END) AS PTSegment,

                               Max(CASE NdtType WHEN 'PWHT' THEN RequestNumber END) AS MasterPWHTRequestNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN NdtReportNumber END) AS ContractorPWHTRequestNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN ResponseReportNumber END) AS ContractorPWHTReportNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN ResponseReportDatetime END) AS PWHTDate,
                           Max(CASE NdtType WHEN 'PWHT' THEN Defect END) AS PWHTDefect,
                           Max(CASE NdtType WHEN 'PWHT' THEN Remark END) AS PWHTSegment,

                                   Max(CASE NdtType WHEN 'MT' THEN RequestNumber END) AS MasterMTRequestNumber,
                           Max(CASE NdtType WHEN 'MT' THEN NdtReportNumber END) AS ContractorMTRequestNumber,
                           Max(CASE NdtType WHEN 'MT' THEN ResponseReportNumber END) AS ContractorMTReportNumber,
                           Max(CASE NdtType WHEN 'MT' THEN ResponseReportDatetime END) AS MTDate,
                           Max(CASE NdtType WHEN 'MT' THEN Defect END) AS MTDefect,
                           Max(CASE NdtType WHEN 'MT' THEN Remark END) AS MTSegment,

                                           Max(CASE NdtType WHEN 'UT' THEN RequestNumber END) AS MasterUTRequestNumber,
                           Max(CASE NdtType WHEN 'UT' THEN NdtReportNumber END) AS ContractorUTRequestNumber,
                           Max(CASE NdtType WHEN 'UT' THEN ResponseReportNumber END) AS ContractorUTReportNumber,
                           Max(CASE NdtType WHEN 'UT' THEN ResponseReportDatetime END) AS UTDate,
                           Max(CASE NdtType WHEN 'UT' THEN Defect END) AS UTDefect,
                           Max(CASE NdtType WHEN 'UT' THEN Remark END) AS UTSegment


                    FROM   (

SELECT t.NdtType,
       t.RequestNumber,
       t.NdtReportNumber,
       t.ResponseReportDatetime,
       t.Defect,
       t.ResponseReportNumber,
       t.Remark
FROM
(
    SELECT NRD.NdtType,
           NRD.Id,
           NRD.RequestNumber,
           NR.NdtReportNumber,
           NRD.ResponseReportDatetime,
           NRD.Defect,
           NRD.ResponseReportNumber,
           NRD.Remark,
           ROW_NUMBER() OVER(PARTITION BY NRD.NdtType ORDER BY NRD.Id DESC) AS rn
    FROM dbo.NdtReportDetails NRD
    LEFT OUTER JOIN NdtReports NR
        ON NRD.ReportId = NR.Id
    WHERE NRD.JointId = c.Id AND
          NRD.NdtType IN ('RT', 'PT', 'PWHT', 'MT', 'UT')
) t
WHERE t.rn = 1
ORDER BY t.Id DESC

) i)b

正如您所看到的,根据上述问题更改了查询结束,执行此查询后,我收到此错误:

  

Msg 1033,Level 15,State 1,Procedure SPJointHistory,Line 72
  除非还指定了TOP,OFFSET或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

1 个答案:

答案 0 :(得分:0)

正如错误所说,你在内部查询中有一个ORDER BY。因为你没有显示id我不认为按id排序它是有道理的。因此,您可以删除订单并将内部查询转换为CTE并按以下方式执行:

;WITH ReportTable
AS(
   SELECT t.NdtType,
       t.RequestNumber,
       t.NdtReportNumber,
       t.ResponseReportDatetime,
       t.Defect,
       t.ResponseReportNumber,
       t.Remark
FROM
(
    SELECT NRD.NdtType,
           NRD.Id,
           NRD.RequestNumber,
           NR.NdtReportNumber,
           NRD.ResponseReportDatetime,
           NRD.Defect,
           NRD.ResponseReportNumber,
           NRD.Remark,
           ROW_NUMBER() OVER(PARTITION BY NRD.NdtType ORDER BY NRD.Id DESC) AS rn
    FROM dbo.NdtReportDetails NRD
    LEFT OUTER JOIN NdtReports NR
        ON NRD.ReportId = NR.Id
    WHERE NRD.JointId = c.Id AND
          NRD.NdtType IN ('RT', 'PT', 'PWHT', 'MT', 'UT')
) t
WHERE t.rn = 1
)


SELECT *
FROM   dbo.JointHistory c
       OUTER Apply (SELECT Max(CASE NdtType WHEN 'RT' THEN RequestNumber END) AS MasterRTRequestNumber,
                           Max(CASE NdtType WHEN 'RT' THEN NdtReportNumber END) AS ContractorRTRequestNumber,
                           Max(CASE NdtType WHEN 'RT' THEN ResponseReportNumber END) AS ContractorRTReportNumber,
                           Max(CASE NdtType WHEN 'RT' THEN ResponseReportDatetime END) AS RTDate,
                           Max(CASE NdtType WHEN 'RT' THEN Defect END) AS RTDefect,
                           Max(CASE NdtType WHEN 'RT' THEN Remark END) AS RTSegment,

                           Max(CASE NdtType WHEN 'PT' THEN RequestNumber END) AS MasterPTRequestNumber,
                           Max(CASE NdtType WHEN 'PT' THEN NdtReportNumber END) AS ContractorPTRequestNumber,
                           Max(CASE NdtType WHEN 'PT' THEN ResponseReportNumber END) AS ContractorPTReportNumber,
                           Max(CASE NdtType WHEN 'PT' THEN ResponseReportDatetime END) AS PTDate,
                           Max(CASE NdtType WHEN 'PT' THEN Defect END) AS PTDefect,
                           Max(CASE NdtType WHEN 'PT' THEN Remark END) AS PTSegment,

                               Max(CASE NdtType WHEN 'PWHT' THEN RequestNumber END) AS MasterPWHTRequestNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN NdtReportNumber END) AS ContractorPWHTRequestNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN ResponseReportNumber END) AS ContractorPWHTReportNumber,
                           Max(CASE NdtType WHEN 'PWHT' THEN ResponseReportDatetime END) AS PWHTDate,
                           Max(CASE NdtType WHEN 'PWHT' THEN Defect END) AS PWHTDefect,
                           Max(CASE NdtType WHEN 'PWHT' THEN Remark END) AS PWHTSegment,

                                   Max(CASE NdtType WHEN 'MT' THEN RequestNumber END) AS MasterMTRequestNumber,
                           Max(CASE NdtType WHEN 'MT' THEN NdtReportNumber END) AS ContractorMTRequestNumber,
                           Max(CASE NdtType WHEN 'MT' THEN ResponseReportNumber END) AS ContractorMTReportNumber,
                           Max(CASE NdtType WHEN 'MT' THEN ResponseReportDatetime END) AS MTDate,
                           Max(CASE NdtType WHEN 'MT' THEN Defect END) AS MTDefect,
                           Max(CASE NdtType WHEN 'MT' THEN Remark END) AS MTSegment,

                                           Max(CASE NdtType WHEN 'UT' THEN RequestNumber END) AS MasterUTRequestNumber,
                           Max(CASE NdtType WHEN 'UT' THEN NdtReportNumber END) AS ContractorUTRequestNumber,
                           Max(CASE NdtType WHEN 'UT' THEN ResponseReportNumber END) AS ContractorUTReportNumber,
                           Max(CASE NdtType WHEN 'UT' THEN ResponseReportDatetime END) AS UTDate,
                           Max(CASE NdtType WHEN 'UT' THEN Defect END) AS UTDefect,
                           Max(CASE NdtType WHEN 'UT' THEN Remark END) AS UTSegment
                     FROM  ReportTable  i)b
--ORDER BY b.Id DESC