Оптимизировать запрос, который вычисляет год и дату за предыдущий год - PullRequest
1 голос
/ 22 марта 2019

У меня есть следующий запрос, который вычисляет год к дате и предыдущий год сегодня.Как я могу его оптимизировать?

;

WITH grouped_by_date AS
  (SELECT *
   FROM tmp.FACT_DC s),
     cumulative_sum_for_ytd AS
  (SELECT T.*,
          T1.QTY_UoM_YTD,t1.QTY_YTD,t1.AMOUNT_LCPNV
   FROM grouped_by_date T CROSS APPLY
     (SELECT SUM(QTY_UoM) AS QTY_UoM_YTD,SUM(QTY) AS QTY_YTD, SUM(PNV_LC)as AMOUNT_LCPNV
      FROM grouped_by_date
      WHERE [Delivery_Year]=T.[Delivery_Year]
        AND convert(date,Delivery_Year+'-'+ Delivery_month+'-'+ [Invoicing_Day])<=convert(date,T.Delivery_Year+'-'+ t.Delivery_month+'-'+ t.[Invoicing_Day])
        AND [Sales_Organization]=T.Sales_Organization
        AND[Sales_Product_Name_N3]=T.Sales_Product_Name_N3
        AND[Sales_Product_Name_N2]=T.[Sales_Product_Name_N2]
        AND[Sales_Product_Name_N1]=T.[Sales_Product_Name_N1]
        AND[Market_Segment_Name_N2]=T.[Market_Segment_Name_N2]
        AND[Doc_Currency]=T.Doc_Currency
        AND[User_Name_N1]=T.User_Name_N1
        AND[Engineer_Code]=T.Engineer_Code
        AND[User_Name_N2]=T.User_Name_N2
        AND[Bill_To_Customer_Code]=T.Bill_To_Customer_Code
        AND[User_Country_Code]=T.User_Country_Code
        AND[User_N3_Country_Name]=T.User_N3_Country_Name ) T1)
SELECT lastYear.*,
  (SELECT SUM(thisYear.QTY_UoM) AS QTY_UoM_YTD_prev
   FROM cumulative_sum_for_ytd thisYear
   WHERE convert(int,thisYear.Delivery_Year) = convert(int,lastYear.Delivery_Year - 1)
     AND (convert(int,thisYear.Delivery_Month) < convert(int,lastYear.Delivery_Month)
          OR convert(int,thisYear.Delivery_Month) = convert(int,lastYear.Delivery_Month)
          AND convert(int,thisYear.Invoicing_Day) <- convert(int,lastYear.Invoicing_Day))
     AND thisYear.[Sales_Organization]=lastYear.Sales_Organization
     AND thisYear.[Sales_Product_Name_N3]=lastYear.Sales_Product_Name_N3
     AND thisYear.[Sales_Product_Name_N2]=lastYear.[Sales_Product_Name_N2]
     AND thisYear.[Sales_Product_Name_N1]=lastYear.[Sales_Product_Name_N1]
     AND thisYear.[Market_Segment_Name_N2]=lastYear.[Market_Segment_Name_N2]
     AND thisYear.[Doc_Currency]=lastYear.Doc_Currency
     AND thisYear.[User_Name_N1]=lastYear.User_Name_N1
     AND thisYear.[Engineer_Code]=lastYear.Engineer_Code
     AND thisYear.[User_Name_N2]=lastYear.User_Name_N2
     AND thisYear.[Bill_To_Customer_Code]=lastYear.Bill_To_Customer_Code
     AND thisYear.[User_Country_Code]=lastYear.User_Country_Code
     AND thisYear.[User_N3_Country_Name]=lastYear.User_N3_Country_Name )AS QTY_UoM_YTD_prev,
     (SELECT SUM(thisYear.QTY) 
   FROM cumulative_sum_for_ytd thisYear
   WHERE convert(int,thisYear.Delivery_Year) = convert(int,lastYear.Delivery_Year - 1)
     AND (convert(int,thisYear.Delivery_Month) < convert(int,lastYear.Delivery_Month)
          OR convert(int,thisYear.Delivery_Month) = convert(int,lastYear.Delivery_Month)
          AND convert(int,thisYear.Invoicing_Day) <- convert(int,lastYear.Invoicing_Day))
     AND thisYear.[Sales_Organization]=lastYear.Sales_Organization
  AND thisYear.[Sales_Product_Name_N3]=lastYear.Sales_Product_Name_N3
     AND thisYear.[Sales_Product_Name_N2]=lastYear.[Sales_Product_Name_N2]
     AND thisYear.[Sales_Product_Name_N1]=lastYear.[Sales_Product_Name_N1]
     AND thisYear.[Market_Segment_Name_N2]=lastYear.[Market_Segment_Name_N2]
     AND thisYear.[Doc_Currency]=lastYear.Doc_Currency
     AND thisYear.[User_Name_N1]=lastYear.User_Name_N1
     AND thisYear.[Engineer_Code]=lastYear.Engineer_Code
     AND thisYear.[User_Name_N2]=lastYear.User_Name_N2
     AND thisYear.[Bill_To_Customer_Code]=lastYear.Bill_To_Customer_Code
     AND thisYear.[User_Country_Code]=lastYear.User_Country_Code
     AND thisYear.[User_N3_Country_Name]=lastYear.User_N3_Country_Name )AS

QTY_YTD_prev, (ВЫБРАТЬ СУММУ (thisYear.AMOUNT_LCPNV) ОТ cumulative_sum_for_ytd thisYear

WHERE convert(int,thisYear.Delivery_Year) = convert(int,lastYear.Delivery_Year - 1)
     AND (convert(int,thisYear.Delivery_Month) < convert(int,lastYear.Delivery_Month)
          OR convert(int,thisYear.Delivery_Month) = convert(int,lastYear.Delivery_Month)
          AND convert(int,thisYear.Invoicing_Day) <- convert(int,lastYear.Invoicing_Day))
     AND thisYear.[Sales_Organization]=lastYear.Sales_Organization
     AND thisYear.[Sales_Product_Name_N3]=lastYear.Sales_Product_Name_N3
     AND thisYear.[Sales_Product_Name_N2]=lastYear.[Sales_Product_Name_N2]
     AND thisYear.[Sales_Product_Name_N1]=lastYear.[Sales_Product_Name_N1]
     AND thisYear.[Market_Segment_Name_N2]=lastYear.[Market_Segment_Name_N2]
     AND thisYear.[Doc_Currency]=lastYear.Doc_Currency
     AND thisYear.[User_Name_N1]=lastYear.User_Name_N1
     AND thisYear.[Engineer_Code]=lastYear.Engineer_Code
     AND thisYear.[User_Name_N2]=lastYear.User_Name_N2
     AND thisYear.[Bill_To_Customer_Code]=lastYear.Bill_To_Customer_Code
     AND thisYear.[User_Country_Code]=lastYear.User_Country_Code
     AND thisYear.[User_N3_Country_Name]=lastYear.User_N3_Country_Name )AS

AMOUNT_LCPNV_prev FROM cumulative_

sum_for_ytd la

stYear

1 Ответ

0 голосов
/ 22 марта 2019

Вместо использования CTE вы можете реорганизовать свой код, используя временные таблицы.Установка правильных индексов на Temps также ускорит.

Например:

    drop table if exists #cumulative_sum_for_ytd;

    SELECT T.*,
       T1.QTY_UoM_YTD,t1.QTY_YTD,t1.AMOUNT_LCPNV
INTO #cumulative_sum_for_ytd
FROM tmp.FACT_DC  T
CROSS APPLY
(
    SELECT SUM(QTY_UoM) AS QTY_UoM_YTD,SUM(QTY) AS QTY_YTD, SUM(PNV_LC)as AMOUNT_LCPNV
    FROM tmp.FACT_DC 
    WHERE [Delivery_Year]=T.[Delivery_Year]
        AND convert(date,Delivery_Year+'-'+ Delivery_month+'-'+ [Invoicing_Day])<=convert(date,T.Delivery_Year+'-'+ t.Delivery_month+'-'+ t.[Invoicing_Day])
        AND [Sales_Organization]=T.Sales_Organization
        AND[Sales_Product_Name_N3]=T.Sales_Product_Name_N3
        AND[Sales_Product_Name_N2]=T.[Sales_Product_Name_N2]
        AND[Sales_Product_Name_N1]=T.[Sales_Product_Name_N1]
        AND[Market_Segment_Name_N2]=T.[Market_Segment_Name_N2]
        AND[Doc_Currency]=T.Doc_Currency
        AND[User_Name_N1]=T.User_Name_N1
        AND[Engineer_Code]=T.Engineer_Code
        AND[User_Name_N2]=T.User_Name_N2
        AND[Bill_To_Customer_Code]=T.Bill_To_Customer_Code
        AND[User_Country_Code]=T.User_Country_Code
        AND[User_N3_Country_Name]=T.User_N3_Country_Name
) T1

и т.д ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...