1 2 3 4 5 6 7 8 9 10 11 12 13
| SELECT M0.SalesDoc, M0.SalesDate, MAX(M0.BuyDate) AS LastProcDate, DATEDIFF(d,MAX(M0.BuyDate),M0.SalesDate) AS Delay, M0.[ItemCode], M0.[Dscription], M0.[Quantity], M0.[PriceBefDi], M0.LineDiscnt, M0.[LineTotal], M0.DocIdscnt, M0.[Project] FROM
(SELECT T0.[DocEntry] AS SalesDoc,T1.[DocDate] AS SalesDate, T0.[ItemCode], T0.[Dscription], T0.[Quantity], T0.[PriceBefDi], T0.[DiscPrcnt] AS LineDiscnt, T0.[LineTotal], T1.[DiscPrcnt] AS DocIdscnt, T0.[Project], S0.[DocDate] AS BuyDate, S0.[DocEntry] AS BuyDoc
FROM INV1 T0 INNER JOIN OINV T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T0.[ItemCode] = T2.[ItemCode]
INNER JOIN
(SELECT T0.[DocEntry], T1.[DocDate], T0.[ItemCode], T0.[Quantity],
CASE WHEN T1.[DiscPrcnt] IS NOT NULL THEN [LineTotal]*(1-T1.[DiscPrcnt]/100)
ELSE T0.[LineTotal]
END AS Total, T0.[Project]
FROM POR1 T0 INNER JOIN OPOR T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T0.[ItemCode] = T2.[ItemCode]
WHERE (T2.[ItmsGrpCod] ='110' OR T2.[ItmsGrpCod] ='111') AND T0.[Project] LIKE ('S%')) S0
ON T0.[ItemCode] = S0.[ItemCode] AND T0.[Project]=S0.[Project]
WHERE T1.[U_IFC_TDD] ='Service' AND T0.[Project] NOT IN ('@SP', '@SVC') AND (T2.[ItmsGrpCod] ='110' OR T2.[ItmsGrpCod] ='111') AND S0.[DocDate] < T1.[DocDate]) M0
GROUP BY M0.SalesDoc, M0.SalesDate, M0.[ItemCode], M0.[Dscription], M0.[Quantity], M0.[PriceBefDi], M0.LineDiscnt, M0.[LineTotal], M0.DocIdscnt, M0.[Project] |
Partager