SELECT LastPO.LastPONo AS PONo, LastPO.LastVersion, dbo.[CRONUS$Purchase Header Archive].[Currency Code],
dbo.[CRONUS$Purchase Header Archive].[Buy-from Vendor No_], dbo.[CRONUS$Purchase Header Archive].[Pay-to Vendor No_],
dbo.[CRONUS$Purchase Header Archive].[Pay-to Name], dbo.[CRONUS$Purchase Header Archive].[Order Date],
dbo.[CRONUS$Purchase Header Archive].[Document Date], dbo.[CRONUS$Purchase Header Archive].[Archived By],
dbo.[CRONUS$Purchase Header Archive].[No_ Printed], dbo.[CRONUS$Purchase Header Archive].[Receiving No_],
SUM(dbo.[CRONUS$Purchase Line Archive].Quantity) AS Qty, SUM(dbo.[CRONUS$Purchase Line Archive].Amount) AS Amt,
SUM(dbo.[CRONUS$Purchase Line Archive].[Amount Including VAT]) AS AmtIncVat
FROM dbo.[CRONUS$Purchase Header Archive] INNER JOIN
(SELECT [Document Type], No_ AS LastPONo, MAX([Version No_]) AS LastVersion
FROM dbo.[CRONUS$Purchase Header Archive] AS [CRONUS$Purchase Header Archive_1]
GROUP BY [Document Type], No_
HAVING ([Document Type] = 1)) AS LastPO ON dbo.[CRONUS$Purchase Header Archive].No_ = LastPO.LastPONo AND
dbo.[CRONUS$Purchase Header Archive].[Version No_] = LastPO.LastVersion INNER JOIN
dbo.[CRONUS$Purchase Line Archive] ON
dbo.[CRONUS$Purchase Header Archive].[Document Type] = dbo.[CRONUS$Purchase Line Archive].[Document Type] AND
dbo.[CRONUS$Purchase Header Archive].No_ = dbo.[CRONUS$Purchase Line Archive].[Document No_] AND
dbo.[CRONUS$Purchase Header Archive].[Version No_] = dbo.[CRONUS$Purchase Line Archive].[Version No_]
GROUP BY dbo.[CRONUS$Purchase Header Archive].[Currency Code], dbo.[CRONUS$Purchase Header Archive].[Buy-from Vendor No_],
dbo.[CRONUS$Purchase Header Archive].[Pay-to Vendor No_], dbo.[CRONUS$Purchase Header Archive].[Pay-to Name],
dbo.[CRONUS$Purchase Header Archive].[Order Date], dbo.[CRONUS$Purchase Header Archive].[Document Date],
dbo.[CRONUS$Purchase Header Archive].[Archived By], dbo.[CRONUS$Purchase Header Archive].[No_ Printed],
dbo.[CRONUS$Purchase Header Archive].[Receiving No_], LastPO.LastPONo, LastPO.LastVersion
HAVING (dbo.[CRONUS$Purchase Header Archive].[Order Date] >= CONVERT(DATETIME, '2016-10-01 00:00:00', 102)) AND
(dbo.[CRONUS$Purchase Header Archive].[Order Date] <= CONVERT(DATETIME, '2017-06-30 00:00:00', 102))
No comments:
Post a Comment