更好的方法来编写这个sql - CTE / Temp表

时间:2015-03-13 20:56:55

标签: sql tsql common-table-expression

对于如何以更有效的方式编写此代码,我真的可以提供一些帮助。我基本上创建了一个CTE来保存包含车辆的数据集,按日和它们装载的集装箱的重量。容器需要具有一定的重量,因此通过/失败字段。

我所要做的就是取得CTE中的升力水平细节,然后将其汇总,以获得通过/未通过标准的提升集装箱的数量,并汇总任何失败升降机的原因。

我只是想知道最好的方法是使用CTE,或者临时表或任何方法是否会加速它或更标准的做法?

理想情况下,我会发布CTE数据的图像,然后发布输出数据,但我无法发布图像:(

非常感谢提前

WITH DetailLifts
     AS (SELECT CO.Description                      'Outlet',
                LE.[VehicleCode],
                S.FORENAME + ' ' + S.SURNAME        AS Name,
                le.[CollectionDate],
                le.lifteventid,
                C.CustomerName,
                Cast([CollectionTimeStamp] AS TIME) 'CollectionTime',
                LE.[NetWeight],
                le.grossweight,
                le.tareweight,
                ct.description,
                CASE
                  WHEN CT.Description LIKE 'Euro%' THEN 10
                  WHEN CT.Description LIKE 'FEL%' THEN 30
                END                                 'Threshold',
                CASE
                  WHEN le.Grossweight IS NOT NULL
                       AND le.grossweight - le.tareweight < ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN -10
                                                                WHEN CT.Description LIKE 'FEL%' THEN -30
                                                              END )
                        OR le.grossweight - le.tareweight > ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN 10
                                                                WHEN CT.Description LIKE 'FEL%' THEN 30
                                                              END ) THEN 1
                  WHEN le.grossweight IS NULL
                       AND le.netweight < ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN -10
                                              WHEN CT.Description LIKE 'FEL%' THEN -30
                                            END )
                        OR le.netweight > ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN 10
                                              WHEN CT.Description LIKE 'FEL%' THEN 30
                                            END ) THEN 1
                  WHEN le.netweight IS NULL THEN 0
                  WHEN le.grossweight = 0
                       AND le.tareweight = 0
                       AND le.netweight = 0 THEN 0
                  ELSE 0
                END                                 'Overweight',
                CASE
                  WHEN Isnull([LiftText], 'No') = 'No' THEN 1
                  ELSE 0
                END                                 AS 'NoLift',
                CASE
                  WHEN Isnull([LiftText], 'No') = 'no' THEN 0
                  ELSE 1
                END                                 AS 'Lifts',
                CASE
                  WHEN SOI.SiteOrderId IS NULL THEN 'Not Matched'
                  ELSE 'Matched'
                END                                 AS 'MatchedLogic',
                CASE
                  WHEN Isnull([LiftText], 'No') = 'No'
                       AND LiftInformationId IS NOT NULL THEN 'Yes'
                  ELSE 'No'
                END                                 'Reason Code Used',
                r2.Description                      AS 'ReasonCode',
                CASE
                  WHEN le.Grossweight IS NOT NULL
                       AND le.grossweight - le.tareweight < ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN -10
                                                                WHEN CT.Description LIKE 'FEL%' THEN -30
                                                              END )
                        OR le.grossweight - le.tareweight > ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN 10
                                                                WHEN CT.Description LIKE 'FEL%' THEN 30
                                                              END ) THEN 0
                  WHEN le.grossweight IS NULL
                       AND le.netweight < ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN -10
                                              WHEN CT.Description LIKE 'FEL%' THEN -30
                                            END )
                        OR le.netweight > ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN 10
                                              WHEN CT.Description LIKE 'FEL%' THEN 30
                                            END ) THEN 0
                  WHEN ( Substring(le.vehiclecode, 3, 2) IN ( 57, 58, 63, 14,
                                                              64, 15, 65 )
                          OR ct.description LIKE '%fel%' )
                       AND le.netweight IS NOT NULL
                       AND le.grossweight IS NULL THEN 0
                  WHEN le.netweight IS NULL
                        OR ( le.grossweight = 0
                             AND le.tareweight = 0 ) THEN 0
                  ELSE 1
                END                                 'Pass',
                CASE
                  WHEN le.Grossweight IS NOT NULL
                       AND le.grossweight - le.tareweight < ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN -10
                                                                WHEN CT.Description LIKE 'FEL%' THEN -30
                                                              END )
                        OR le.grossweight - le.tareweight > ( CASE
                                                                WHEN CT.Description LIKE 'Euro%' THEN 10
                                                                WHEN CT.Description LIKE 'FEL%' THEN 30
                                                              END ) THEN 1
                  WHEN le.grossweight IS NULL
                       AND le.netweight < ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN -10
                                              WHEN CT.Description LIKE 'FEL%' THEN -30
                                            END )
                        OR le.netweight > ( CASE
                                              WHEN CT.Description LIKE 'Euro%' THEN 10
                                              WHEN CT.Description LIKE 'FEL%' THEN 30
                                            END ) THEN 1
                  WHEN ( Substring(le.vehiclecode, 3, 2) IN ( 57, 58, 63, 14,
                                                              64, 15, 65 )
                          OR ct.description LIKE '%fel%' )
                       AND le.netweight IS NOT NULL
                       AND le.grossweight IS NULL THEN 1
                  WHEN le.netweight IS NULL
                        OR ( le.grossweight = 0
                             AND le.tareweight = 0 ) THEN 1
                  ELSE 0
                END                                 'Fail',
                lifttext,
                CASE
                  WHEN ( Substring(le.vehiclecode, 3, 2) IN ( 57, 58, 63, 14,
                                                              64, 15, 65 )
                          OR ct.description LIKE '%fel%' )
                       AND le.netweight IS NOT NULL
                       AND le.grossweight IS NULL THEN 1
                  ELSE 0
                END                                 AS 'VehicleFault'
         FROM   [CFO_P155_Pre_Production_ELEMOS].[dbo].[LiftEvent]LE
                LEFT JOIN CFO_P155_Pre_Production_ELEMOS.dbo.Route RT
                       ON RT.routeid = le.routeid
                LEFT JOIN CFO_P155_Pre_Production_ELEMOS.dbo.SysUser S
                       ON S.sysuserid = RT.driversysuserid
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Vehicle]V
                       ON LE.VehicleId = V.VehicleId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[CompanyOutlet]CO
                       ON V.CompanyOutletId = CO.CompanyOutletId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Reason]R
                       ON LE.LiftProblemId = R.ReasonId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Reason]R2
                       ON LE.LiftInformationId = R2.ReasonId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[SiteOrderItem]SOI
                       ON SOI.SiteOrderItemId = LE.SiteOrderItemId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[SiteOrder]SO
                       ON SOI.SiteOrderId = SO.SiteOrderId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[CustomerSite]CS
                       ON SO.CustomerSiteId = CS.CustomerSiteId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Location]L
                       ON CS.LocationId = L.LocationId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Customer]C
                       ON CS.CustomerId = C.CustomerId
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[Material]M
                       ON SO.MaterialId = M.Materialid
                LEFT JOIN [CFO_P155_Pre_Production_ELEMOS].[dbo].[ContainerType]CT
                       ON SOI.ContainerTypeId = CT.ContainerTypeId
         WHERE  CO.Description IN ( 'Portsmouth' )
                AND le.CollectionDate >= '2015-03-02'
                AND le.CollectionDate <= '2015-03-06'
                AND ( ct.description LIKE '%euro%'
                       OR ct.description LIKE '%FEl%' )
                --and substring(le.vehiclecode,3,2) in (57,58,63,64,65) and le.netweight is         not null and le.grossweight is null
                AND C.Customerid = 18407)
SELECT dl.outlet,
       dl.collectiondate,
       dl.vehiclecode,
       dl.name,
       CASE
         WHEN passesandfails.Pass >= 5
              AND passesandfails.Fail = 0 THEN 'Pass'
         ELSE 'Fail'
       END                                        AS 'Pass or Fail',
       passesandfails.pass,
       passesandfails.fail,
       Isnull(matchedlifts.matchedActuallifts, 0) AS MatchedActualLifts,
       Isnull(lifted.NoLift, 0)                   AS 'MatchedNotLifted(Lift  Complete)',
       Isnull(overweights.overweight, 0)          AS Overweights,
       Sum(vehiclefault)                          AS VehicleFault
FROM   DetailLifts DL
       LEFT JOIN (SELECT outlet,
                         collectiondate,
                         vehiclecode,
                         Sum(pass) pass,
                         Sum(fail) fail
                  FROM   detaillifts dl
                  GROUP  BY outlet,
                            collectiondate,
                            vehiclecode) AS PassesandFails
              ON dl.Collectiondate = passesandfails.collectiondate
                 AND dl.vehiclecode = passesandfails.vehiclecode
       LEFT JOIN (SELECT outlet,
                         collectiondate,
                         vehiclecode,
                         Sum(overweight)AS overweight
                  FROM   detaillifts DL
                  GROUP  BY outlet,
                            collectiondate,
                            vehiclecode) AS Overweights
              ON dl.collectiondate = overweights.collectiondate
                 AND dl.vehiclecode = overweights.vehiclecode
       LEFT JOIN (SELECT collectiondate,
                         vehiclecode,
                         Count(lifteventid) AS MatchedActualLifts
                  FROM   detaillifts DL
                  WHERE  lifttext IS NOT NULL
                  GROUP  BY collectiondate,
                            vehiclecode) AS MatchedLifts
              ON dl.collectiondate = matchedlifts.collectiondate
                 AND dl.vehiclecode = matchedlifts.vehiclecode
       LEFT JOIN (SELECT collectiondate,
                         vehiclecode,
                         Sum(lifts)  AS Lifts,
                         Sum(nolift) AS NoLift
                  FROM   detaillifts
                  GROUP  BY collectiondate,
                            vehiclecode)AS lifted
              ON lifted.collectiondate = dl.collectiondate
                 AND lifted.vehiclecode = dl.vehiclecode
GROUP  BY dl.outlet,
          dl.collectiondate,
          dl.vehiclecode,
          dl.name,
          matchedActuallifts,
          passesandfails.pass,
          passesandfails.fail,
          lifted.nolift,
          overweights.overweight
ORDER  BY outlet,
          collectiondate,
          vehiclecode 

1 个答案:

答案 0 :(得分:0)

CTE只是语法并被评估为 但如果CTE运行速度快,那就不是问题了 实现t #temp的成本 单独运行CTE需要多长时间?
锄头做了整个查询吗?

但你有两个你不需要的联接

将Sum(超重)AS超重移至PassesandFails

将Count(lifteventid)向下移动到提升为Count(lifttext)
null不计算

这将减少1/2

中的连接开销

如果您得到了您期望的答案,那么我没有看到发布数据的目的