У меня есть следующий запрос, который вычисляет год к дате и предыдущий год сегодня.Как я могу его оптимизировать?
;
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