SELECT
[Item No_] AS SDAITM,
MAX([Description]) AS 'IMDSC1',
CAST(SUM([Invoiced Quantity] * -1) AS INTEGER) AS 'SDQTYT',
INVDATE,
CONVERT(char(10), GETDATE(), 103) AS 'DATE',
CONVERT(char(8), GETDATE(), 8) AS 'TIME'
FROM
(
SELECT
dbo.[company$Item Ledger Entry].[Document No_],
dbo.[company$Item Ledger Entry].[Item No_],
dbo.[company$Item].[Description],
dbo.[company$Item Ledger Entry].[Invoiced Quantity],
CONVERT(char(4), YEAR(dbo.[company$Item Ledger Entry].[Last Invoice Date])) + CONVERT(char(2), RIGHT(REPLICATE('0',2)+ LTRIM(MONTH(dbo.[company$Item Ledger Entry].[Last Invoice Date])),2)) AS INVDATE
FROM dbo.[company$Item Ledger Entry]
INNER JOIN dbo.[company$Item] ON
dbo.[company$Item Ledger Entry].[Item No_] = dbo.[company$Item].No_
INNER JOIN dbo.[company$Default Dimension] ON
dbo.[company$Item Ledger Entry].[Item No_] = dbo.[company$Default Dimension].No_
INNER JOIN dbo.[company$Item Category] ON
dbo.[company$Item Ledger Entry].[Item Category Code] = dbo.[company$Item Category].Code
WHERE
dbo.[company$Item Ledger Entry].[Item No_] NOT IN ('10000','10002','40000')
AND
dbo.[company$Item Ledger Entry].[Location Code] NOT IN ('WS', 'WF', 'WS-GU', 'WS-LG', 'GO', 'FR', 'BOO1')
AND
dbo.[company$Item Ledger Entry].[Entry Type] = 1
AND
dbo.[company$Item Ledger Entry].[Document No_] != 'TAKE-ON'
AND
dbo.[company$Default Dimension].[Dimension Value Code] = 'brand'
AND
dbo.[company$Item Category].[Description] = 'product'
) SQ
GROUP BY [Document No_], [Item No_], INVDATE
ORDER BY INVDATE
|