Here is what I've written - I'm trying to get the dates without transactions to just show zero for the field. I've gone to google and talked to everyone I know, this should work - but isn't :?
IF OBJECT_ID('tempdb..#D') IS NOT NULL
DROP TABLE #D;
CREATE TABLE #D
(
[Date] [DATE] NOT NULL,
PRIMARY KEY CLUSTERED ([Date] ASC)
);
DECLARE @CurrentDate DATE = GETDATE() - 95;
DECLARE @EndDate DATE = GETDATE();
WHILE @CurrentDate < @EndDate
BEGIN
INSERT INTO #D
(
[Date]
)
SELECT DATE = @CurrentDate;
SET @CurrentDate = DATEADD(DD, 1, @CurrentDate);
END;
--SELECT * FROM #D ORDER BY Date DESC
SELECT ITEM2_NUM AS 'Part Number',
COALESCE(SUM(TRANS_QTY), 0) 'Daily Usage',
D.Date AS 'Usage Date'
FROM #D AS D
LEFT OUTER JOIN dbo.W_INVENTORY_TRANS_F IT
ON IT.GL_DT = D.Date
LEFT JOIN dbo.W_BU_ITEM_D BI
ON BI.BUSINESS_UNIT_WID = IT.BUSINESS_UNIT_WID
AND BI.ITEM_WID = IT.ITEM_WID
WHERE DOCUMENT_TYPE_WID = 22
AND D.Date >= DATEADD(yy, -1, GETDATE())
AND IT.BUSINESS_UNIT_WID = '837'
AND IT.ITEM2_NUM = '10111'
AND BI.STOCKING_TYPE = 'P'
GROUP BY ITEM2_NUM,
D.Date
ORDER BY D.Date DESC;
Below is the results of what I get:
Part Number Daily Usage Usage Date
10111 -331.0000 2019-08-19
10111 -2617.0000 2019-08-16
10111 -418.0000 2019-08-15
10111 -471.0000 2019-08-14
10111 -1158.0000 2019-08-13
10111 -766.0000 2019-08-12
10111 -1385.0000 2019-08-09
This is what I want:
Part Number Daily Usage Usage Date
10111 -331 8/19/2019
10111 0 8/18/2019
10111 0 8/17/2019
10111 -2617 8/16/2019
10111 -418 8/15/2019
10111 -471 8/14/2019
10111 -1158 8/13/2019
10111 -766 8/12/2019
10111 0 8/11/2019
10111 0 8/10/2019
10111 -1385 8/9/2019