Подзапрос с несколькими столбцами с использованием count, sum и top - PullRequest
0 голосов
/ 15 ноября 2018

Создание следующей временной таблицы:

  CREATE TABLE #BEDRAGEN(Dossiercode T_Code_Dossier,Detailcode T_Code_Detail,DetailSubcode T_Code_DetailSub
                        ,Totaalbedrag T_Fin_Amount,TotGefactDiensten T_Fin_Amount, TotGefactgoederen T_Fin_Amount, Verzonden int
                        ,TotaalInkord T_Fin_Amount, TotaalInkRgls int,Inkooporder T_Nr_Ord, InkOrdRgl T_LineNR)

  INSERT INTO #BEDRAGEN
  SELECT DD.DossierCode as Dossiercode,
         DD.DetailCode as Detailcode,
         DD.DetailSubCode as DetailSubcode,
         ISNULL(DBO.SIF_get_SalesAmountDosDetail(DD.Dossiercode,DD.Detailcode,DD.Detailsubcode),0) as Totaalbedrag,
         ISNULL((SELECT SUM(ID.InvDetailAmount)
                FROM T_InvoiceDetailDosDet as IDD
                inner join T_InvoiceDetail as ID on ID.InvCode=IDD.InvCode and ID.InvLineNr=IDD.InvLinenr
                WHERE IDD.DossierCode=DD.Dossiercode AND IDD.DetailCode=DD.Detailcode AND IDD.DetailSubCode=DD.DetailSubcode),0) 
                AS TotGefactDiensten,
         ISNULL((SELECT SUM(ID.InvDetailAmount)
                from T_InvoiceDetailShipDet as InvSD
                inner join T_ShippingDetail as SD on SD.ShipDocCode=InvSD.ShipDocCode and SD.ShipLineNr=InvSD.ShipLineNr
                inner join T_InvoiceDetail as ID on ID.InvCode=InvSD.InvCode and ID.InvLineNr=InvSD.InvLinenr
                where SD.DossierCode=DD.Dossiercode AND SD.DetailCode=DD.Detailcode AND SD.DetailSubCode=DD.DetailSubcode),0) as TotGefactgoederen,
         ISNULL((select sum(Verz.DelQty) 
                from dbo.T_DeliveryLine as Verz
                where Verz.DossierCode=DD.Dossiercode and Verz.DetailCode=DD.Detailcode and Verz.DetailSubCode=DD.DetailSubcode),0) as Verzonden,
         ISNULL((select sum(PDPL.BasicCurrTotalAmount) 
                from T_PurDocPartLineDosDetLink AS PDL
                INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
                INNER JOIN T_PurDocPartLine as PDPL on PDPL.PurDocCode=PDL.PurDocCode and PDPL.PDPartLineNr=PDL.PDPartLineNr
                WHERE PDL.DossierCode=DD.DossierCode AND PDL.DetailCode=DD.DetailCode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS TotaalInkord,
         ISNULL((select COUNT(PDL.DetailCode) 
                from T_PurDocPartLineDosDetLink AS PDL
                INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
                INNER JOIN T_PurDocPartLine as PDPL on PDPL.PurDocCode=PDL.PurDocCode and PDPL.PDPartLineNr=PDL.PDPartLineNr
                WHERE PDL.DossierCode=DD.DossierCode AND PDL.DetailCode=DD.DetailCode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS TotaalInkRgls,
         ISNULL((SELECT TOP 1 PD.PurOrdNr FROM T_PurDocPartLineDosDetLink AS PDL 
                INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
                INNER JOIN T_PurDocPartLine AS INR ON INR.PurDocCode=PDL.PurDocCode AND INR.PDPartLineNr=PDL.PDPartLineNr
                WHERE  PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode),'') AS Inkooporder,
        ISNULL((SELECT TOP 1 PDL.PDPartLineNr FROM T_PurDocPartLineDosDetLink AS PDL 
                INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
                INNER JOIN T_PurDocPartLine AS INR ON INR.PurDocCode=PDL.PurDocCode AND INR.PDPartLineNr=PDL.PDPartLineNr
                WHERE  PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode),0) AS InkOrdRgl
  FROM dbo.T_DossierDetail AS DD
  Inner Join    dbo.T_Part  AS P On P.PartCode = DD.PartCode
  Inner Join    dbo.T_DossierMain  AS DM On DM.DossierCode = DD.DossierCode
  Inner Join    dbo.T_DossierStatus  AS DS On DS.DossierStatusCode = DD.DossierStatusCode
  WHERE    DD.DossierCode > N''

 select * from #bedragen

 drop table #BEDRAGEN

Как вы можете видеть подзапросы для получения столбцов TotaalInkord, TotaalInkRgls, Inkooporder, InkOrdRgl используют то же самое из / innerjoin и где.

Есть ли возможность объединить их? Если объединение возможно, оно будет быстрее?

Спасибо

Справочная информация: Мне нужна эта временная таблица для ускорения производительности хранимой процедуры. Эта хранимая процедура представляет собой большой выбор, использующий ряд функций для получения своих столбцов. Эту функцию-контент я сейчас добавил в temptable.

Ответы [ 2 ]

0 голосов
/ 15 ноября 2018
INSERT INTO #BEDRAGEN
      SELECT DD.DossierCode as Dossiercode,
             DD.DetailCode as Detailcode,
             DD.DetailSubCode as DetailSubcode,
             ISNULL(DBO.SIF_get_SalesAmountDosDetail(DD.Dossiercode,DD.Detailcode,DD.Detailsubcode),0) as Totaalbedrag,
             ISNULL((SELECT SUM(ID.InvDetailAmount)
                    FROM T_InvoiceDetailDosDet as IDD
                    inner join T_InvoiceDetail as ID on ID.InvCode=IDD.InvCode and ID.InvLineNr=IDD.InvLinenr
                    WHERE IDD.DossierCode=DD.Dossiercode AND IDD.DetailCode=DD.Detailcode AND IDD.DetailSubCode=DD.DetailSubcode),0) 
                    AS TotGefactDiensten,
             ISNULL((SELECT SUM(ID.InvDetailAmount)
                    from T_InvoiceDetailShipDet as InvSD
                    inner join T_ShippingDetail as SD on SD.ShipDocCode=InvSD.ShipDocCode and SD.ShipLineNr=InvSD.ShipLineNr
                    inner join T_InvoiceDetail as ID on ID.InvCode=InvSD.InvCode and ID.InvLineNr=InvSD.InvLinenr
                    where SD.DossierCode=DD.Dossiercode AND SD.DetailCode=DD.Detailcode AND SD.DetailSubCode=DD.DetailSubcode),0) as TotGefactgoederen,
             ISNULL((select sum(Verz.DelQty) 
                    from dbo.T_DeliveryLine as Verz
                    where Verz.DossierCode=DD.Dossiercode and Verz.DetailCode=DD.Detailcode and Verz.DetailSubCode=DD.DetailSubcode),0) as Verzonden,
             ISNULL(IO.waarde,0) as TotaalInkord,
             ISNULL(IO.rgls,0) as TotaalInkRgls,
             ISNULL(IO.PurOrdNr,'') as Inkooporder,
             ISNULL(IO.PDPartLineNr,'') as InkOrdRgl
      FROM dbo.T_DossierDetail AS DD
      Inner Join    dbo.T_Part  AS P On P.PartCode = DD.PartCode
      Inner Join    dbo.T_DossierMain  AS DM On DM.DossierCode = DD.DossierCode
      Inner Join    dbo.T_DossierStatus  AS DS On DS.DossierStatusCode = DD.DossierStatusCode
      Inner Join    #StatusType ST on DS.DossierStatusType = ST.StatusType
      OUTER APPLY (SELECT top 1 PD.PurOrdNr,PDL.PDPartLineNr,COUNT(PDL.DetailCode) as rgls,sum(PDPL.BasicCurrTotalAmount) as waarde 
                    FROM T_PurDocPartLineDosDetLink AS PDL 
                    INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
                    INNER JOIN T_PurDocPartLine AS PDPL ON PDPL.PurDocCode=PDL.PurDocCode AND PDPL.PDPartLineNr=PDL.PDPartLineNr
                    WHERE  PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode
                    GROUP BY PD.PurOrdNr,PDL.PDPartLineNr,PDL.DetailCode,PDPL.BasicCurrTotalAmount)  IO
      WHERE    DD.DossierCode > N''
0 голосов
/ 15 ноября 2018

Сложно без выборочных данных, но CROSS APPLY должен сделать свое дело - не могу сказать, будет ли он работать лучше (хотя и должен), и вам, возможно, придется немного его настроить.

CREATE TABLE #BEDRAGEN(Dossiercode T_Code_Dossier,Detailcode T_Code_Detail,DetailSubcode T_Code_DetailSub
                        ,Totaalbedrag T_Fin_Amount,TotGefactDiensten T_Fin_Amount, TotGefactgoederen T_Fin_Amount, Verzonden int
                        ,TotaalInkord T_Fin_Amount, TotaalInkRgls int,Inkooporder T_Nr_Ord, InkOrdRgl T_LineNR)

  INSERT INTO #BEDRAGEN
  SELECT DD.DossierCode as Dossiercode,
         DD.DetailCode as Detailcode,
         DD.DetailSubCode as DetailSubcode,
         ISNULL(DBO.SIF_get_SalesAmountDosDetail(DD.Dossiercode,DD.Detailcode,DD.Detailsubcode),0) as Totaalbedrag,
         sub.TotGefactDiensten,
         sub.TotGefactgoederen,
         sub.Verzonden,
         sub.TotaalInkord,
         sub.TotaalInkRgls,
         sub.Inkooporder,
         sub.InkOrdRgl
  FROM dbo.T_DossierDetail AS DD
  Inner Join    dbo.T_Part  AS P On P.PartCode = DD.PartCode
  Inner Join    dbo.T_DossierMain  AS DM On DM.DossierCode = DD.DossierCode
  Inner Join    dbo.T_DossierStatus  AS DS On DS.DossierStatusCode = DD.DossierStatusCode
  cross apply (
        SELECT 
            ISNULL(SUM(ID.InvDetailAmount), 0) as TotGefactDiensten
            ISNULL(SUM(ID.InvDetailAmount), 0) as as TotGefactgoederen
            ISNULL(sum(Verz.DelQty) ,0) as Verzonden,
            ISNULL(sum(PDPL.BasicCurrTotalAmount) ,0) AS TotaalInkord,
            ISNULL(COUNT(PDL.DetailCode) ,0) AS TotaalInkRgls,
            ISNULL(min(PD.PurOrdNr), '') AS Inkooporder,  -- assuming this PD.PurOrdNr is the same for all records of subquery ???
            ISNULL(min(PDL.PDPartLineNr),0) AS InkOrdRgl
        FROM T_PurDocPartLineDosDetLink AS PDL 
        INNER JOIN T_PurchaseDocument as PD on PDL.PurDocCode=PD.PurDocCode
        INNER JOIN T_PurDocPartLine AS INR ON INR.PurDocCode=PDL.PurDocCode AND INR.PDPartLineNr=PDL.PDPartLineNr
        WHERE  PDL.DossierCode=DD.Dossiercode AND PDL.DetailCode=DD.Detailcode AND PDL.DetailSubCode=DD.DetailSubCode
    ) sub

WHERE    DD.DossierCode > N''

 select * from #bedragen

 drop table #BEDRAGEN
...