MsSQL Query - 简化代码

时间:2017-02-14 06:34:54

标签: sql-server sql-server-2008

我有以下代码,但运行时间太长。有没有办法简化它?我需要iotransactiondate取决于两个不同列上的iostatus,这就是为什么我必须两次加入相同的表格。

 SELECT 
       pg.pgrpName1          [Santiye],
       p.prsncode            [Sicil No],
       p.[prsnname1]         [Adi],
       p.[prsnname2]         [Soyadi],
       CLT.clntName1         [Firmasi],
       fg3.grp3Name1         [Gorevi],
       prf.pcntrName1        [Ekibi],
       lnk11.lgrp11Name1     [Kaldigi Yer],
       lnk12.lgrp12Name1 +' - '+lnk12.lgrp12Name2 [Kamp/Adres],
       lnk13.lgrp13Name1     [Oda No],
       ttt.[iotransactiondate] [Giris Tarihi/Saati],
       tt.[iotransactiondate] [Cikis Tarihi/Saati],
       prsnEText4 [Vardiya],
       tz.tzoneName1 [GECE/GUNDUZ]
       --ps.psStartDate,
       --ps.psFinishDate,
       --[Giris/Cikis] = ( CASE
                          -- WHEN [t.iostatus] = 0 THEN 'Giris'
                          -- WHEN [t.iostatus] = 1 THEN 'Cikis'
                         --  ELSE 'Uzaya Gitti'
                      --   END )
FROM   [Exen].[dbo].[IOTransaction] t
       LEFT JOIN dbo.person p
              ON t.ioPrsnRefId = p.prsnRefId
       LEFT JOIN dbo.PersonShift ps
              ON ps.psPrsnRefId = p.prsnRefId
       LEFT JOIN dbo.TimeZoneMess tz
              ON tz.tzoneRefId = ps.psTzoneRefId
       LEFT JOIN dbo.[PersonGroup] pg
              ON pg.pgrpRefId = p.prsnPgrpRefId 
       LEFT JOIN FreeGroup3 fg3
              ON fg3.grp3RefId = p.prsnGrp3RefId
       left join Client CLT
              ON CLT.clntRefId = P.prsnClntRefId
       LEFT JOIN [ProfitCenter] prf
              ON prf.pcntrRefId = p.prsnPcntrRefId  
       LEFT JOIN LinkedGroup11 lnk11
              ON lnk11.lgrp11RefId = p.prsnLgrp11RefId  
       LEFT JOIN LinkedGroup12 lnk12
              ON lnk12.lgrp12RefId = p.prsnLgrp12RefId  
       LEFT JOIN LinkedGroup13 lnk13
              ON lnk13.lgrp13RefId = p.prsnLgrp13RefId
       LEFT JOIN [Exen].[dbo].[IOTransaction] tt
              ON t.ioPrsnRefId = tt.ioPrsnRefId and tt.[iostatus] = 1     
       LEFT JOIN [Exen].[dbo].[IOTransaction] ttt
              ON t.ioPrsnRefId = ttt.ioPrsnRefId    and ttt.[iostatus] = 0    

WHERE  ( t.[iotransactiondate] = (SELECT Min(m.[ioTransactionDate])
                                  FROM   IOTransaction m
                                  WHERE  m.ioPrsnRefId = t.ioPrsnRefId
                                         AND Cast(m.[iotransactiondate] AS DATE)
                                             = Cast
                                             (
                                             t.[iotransactiondate] AS DATE)
                                  GROUP  BY m.ioPrsnRefId)
          OR t.[iotransactiondate] = (SELECT Max(m.[iotransactiondate])
                                      FROM   IOTransaction m
                                      WHERE  m.ioPrsnRefId = t.ioPrsnRefId
                                             AND Cast(m.[iotransactiondate] AS
                                                      DATE) =
                                                 Cast(
                                                 t.[iotransactiondate] AS DATE)
                                      GROUP  BY m.ioPrsnRefId) )
       AND p.[prsnname1] IS NOT NULL
       AND t.iotransactiondate > '01.12.2016 00:00:00.000'
       AND ps.psStartDate <= t.[iotransactiondate]
       AND ps.psFinishDate > t.[iotransactiondate] 
       --and p.[prsnname1] ='NAIM'
       AND tz.tzoneRefId =4
GROUP  BY pg.pgrpName1  ,
          t.ioPrsnRefId,
          prsncode,
          prsnname1,
          prsnname2,
          t.[iotransactiondate],
          tt.[iotransactiondate],
          ttt.[iotransactiondate],
          t.iostatus,
          tz.tzoneName1,
          ps.psStartDate,
          ps.psFinishDate,
          prsnEText4,
          fg3.grp3Name1,
          CLT.clntName1,
          prf.pcntrName1,
          lgrp11Name1,
          lgrp12Name1,
          lgrp12Name2,
          lgrp13Name1
ORDER  BY P.prsncode, t.iotransactiondate desc

特别是这部分我猜的时间太长了,但我找不到另一种方式。

LEFT JOIN [Exen].[dbo].[IOTransaction] tt
                  ON t.ioPrsnRefId = tt.ioPrsnRefId and tt.[iostatus] = 1     
           LEFT JOIN [Exen].[dbo].[IOTransaction] ttt
                  ON t.ioPrsnRefId = ttt.ioPrsnRefId    and ttt.[iostatus] = 0    

1 个答案:

答案 0 :(得分:0)

我删除了连接部分(tt。和ttt。)并使用子查询第二次选择。

 SELECT 
       pg.pgrpName1          [Santiye],
       p.prsncode            [Sicil No],
       p.[prsnname1]         [Adi],
       p.[prsnname2]         [Soyadi],
       CLT.clntName1         [Firmasi],
       fg3.grp3Name1         [Gorevi],
       prf.pcntrName1        [Ekibi],
       lnk11.lgrp11Name1     [Kaldigi Yer],
       lnk12.lgrp12Name1 +' - '+lnk12.lgrp12Name2 [Kamp/Adres],
       lnk13.lgrp13Name1     [Oda No],
       t.[iotransactiondate] [Giris Tarihi/Saati],
       (SELECT 

        t2.[iotransactiondate] 


        FROM   [Exen].[dbo].[IOTransaction] t2



        WHERE  ( t2.[iotransactiondate] = (SELECT Min(m.[ioTransactionDate])
                                  FROM   IOTransaction m
                                  WHERE  m.ioPrsnRefId = t2.ioPrsnRefId
                                         AND Cast(m.[iotransactiondate] AS DATE)
                                             = Cast
                                             (
                                             t2.[iotransactiondate] AS DATE)
                                  GROUP  BY m.ioPrsnRefId)
          OR t2.[iotransactiondate] = (SELECT Max(m.[iotransactiondate])
                                      FROM   IOTransaction m
                                      WHERE  m.ioPrsnRefId = t2.ioPrsnRefId
                                             AND Cast(m.[iotransactiondate] AS
                                                      DATE) =
                                                 Cast(
                                                 t2.[iotransactiondate] AS DATE)
                                      GROUP  BY m.ioPrsnRefId) )
       AND p.[prsnname1] IS NOT NULL
       AND t2.iotransactiondate > '01.12.2016 00:00:00.000'
       AND ps.psStartDate <= t2.[iotransactiondate]
       AND ps.psFinishDate > t2.[iotransactiondate] 
       --and p.[prsnname1] ='NAIM'
       --AND tz.tzoneRefId =4
       and ioStatus = 1
       and cast(t2.ioTransactionDate as date) = cast(t.ioTransactionDate as date) and t.ioPrsnRefId = t2.ioPrsnRefId
GROUP  BY 
          t2.[iotransactiondate]

         )



       AS  [Cikis Tarihi/Saati],
       prsnEText4 [Vardiya],
       tz.tzoneName1 [GECE/GUNDUZ]
       --ps.psStartDate,
       --ps.psFinishDate,
       --[Giris/Cikis] = ( CASE
                          -- WHEN [t.iostatus] = 0 THEN 'Giris'
                          -- WHEN [t.iostatus] = 1 THEN 'Cikis'
                         --  ELSE 'Uzaya Gitti'
                      --   END )
FROM   [Exen].[dbo].[IOTransaction] t
       LEFT JOIN dbo.person p
              ON t.ioPrsnRefId = p.prsnRefId
       LEFT JOIN dbo.PersonShift ps
              ON ps.psPrsnRefId = p.prsnRefId
       LEFT JOIN dbo.TimeZoneMess tz
              ON tz.tzoneRefId = ps.psTzoneRefId
       LEFT JOIN dbo.[PersonGroup] pg
              ON pg.pgrpRefId = p.prsnPgrpRefId 
       LEFT JOIN FreeGroup3 fg3
              ON fg3.grp3RefId = p.prsnGrp3RefId
       left join Client CLT
              ON CLT.clntRefId = P.prsnClntRefId
       LEFT JOIN [ProfitCenter] prf
              ON prf.pcntrRefId = p.prsnPcntrRefId  
       LEFT JOIN LinkedGroup11 lnk11
              ON lnk11.lgrp11RefId = p.prsnLgrp11RefId  
       LEFT JOIN LinkedGroup12 lnk12
              ON lnk12.lgrp12RefId = p.prsnLgrp12RefId  
       LEFT JOIN LinkedGroup13 lnk13
              ON lnk13.lgrp13RefId = p.prsnLgrp13RefId


WHERE  ( t.[iotransactiondate] = (SELECT Min(m.[ioTransactionDate])
                                  FROM   IOTransaction m
                                  WHERE  m.ioPrsnRefId = t.ioPrsnRefId
                                         AND Cast(m.[iotransactiondate] AS DATE)
                                             = Cast
                                             (
                                             t.[iotransactiondate] AS DATE)
                                  GROUP  BY m.ioPrsnRefId)
          OR t.[iotransactiondate] = (SELECT Max(m.[iotransactiondate])
                                      FROM   IOTransaction m
                                      WHERE  m.ioPrsnRefId = t.ioPrsnRefId
                                             AND Cast(m.[iotransactiondate] AS
                                                      DATE) =
                                                 Cast(
                                                 t.[iotransactiondate] AS DATE)
                                      GROUP  BY m.ioPrsnRefId) )
       AND p.[prsnname1] IS NOT NULL
       AND t.iotransactiondate > '01.12.2016 00:00:00.000'
       AND ps.psStartDate <= t.[iotransactiondate]
       AND ps.psFinishDate > t.[iotransactiondate] 
       --and p.[prsnname1] ='NAIM'
       AND tz.tzoneRefId =4
       and ioStatus = 0
GROUP  BY pg.pgrpName1  ,
          t.ioPrsnRefId,
          prsncode,
          prsnname1,
          prsnname2,
          t.[iotransactiondate],
          t.iostatus,
          tz.tzoneName1,
          ps.psStartDate,
          ps.psFinishDate,
          prsnEText4,
          fg3.grp3Name1,
          CLT.clntName1,
          prf.pcntrName1,
          lgrp11Name1,
          lgrp12Name1,
          lgrp12Name2,
          lgrp13Name1