Question : SQL union to fill date data series gaps for SSRS year on year chart

A year-on-year SSRS chart of sales vs daily calendar dates needs data gaps filled to plot correctly.  

SELECT  TM.D_Product.Model_Group, SUM(TM.POS_Daily.Sales_Qty_Offer_Adjusted) AS AdjSaleSum, TM.D_Customer_V.Customer_Name, Calendar_ALT.[Cal Year Day Num],
               Calendar_ALT.[Cal Year]
FROM     TM.POS_Daily LEFT OUTER JOIN
               TM.D_Customer_V ON TM.POS_Daily.Customer_Key = TM.D_Customer_V.Customer_KEY LEFT OUTER JOIN
               TM.D_Product ON TM.POS_Daily.Item_Num = TM.D_Product.Product_Code LEFT OUTER JOIN
               Calendar_ALT ON TM.POS_Daily.Date_KEY = Calendar_ALT.[Cal Mstr Date]
GROUP BY TM.D_Customer_V.Customer_Name, TM.D_Product.Model_Group, Calendar_ALT.[Cal Year Day Num], Calendar_ALT.[Cal Year]
HAVING  (TM.D_Product.Model_Group = @CustMdl) AND (TM.D_Customer_V.Customer_Name = @CustNa)

The parameters are  "@CustNa"

SELECT DISTINCT TM.D_Customer_V.Customer_Name
FROM     TM.D_Customer_V RIGHT OUTER JOIN
               TM.POS_Daily ON TM.D_Customer_V.Customer_KEY = TM.POS_Daily.Customer_Key
ORDER BY TM.D_Customer_V.Customer_Name

and "@CustMdl"

SELECT DISTINCT TM.D_Customer_V.Customer_Name, TM.D_Product.Model_Group
FROM     TM.POS_Daily LEFT OUTER JOIN
               TM.D_Product ON TM.POS_Daily.Item_Num = TM.D_Product.Product_Code LEFT OUTER JOIN
               TM.D_Customer_V ON TM.POS_Daily.Customer_Key = TM.D_Customer_V.Customer_KEY
WHERE  (TM.D_Customer_V.Customer_Name = @CustNa)
ORDER BY TM.D_Customer_V.Customer_Name, TM.D_Product.Model_Group


How are zero values inserted into AdjSaleSum for a Customer and Model selected for the dates having no data?

This query  

SELECT  [Cal Month Use], [Cal Year Day Num], [Cal Year], [Cal Mstr Date]
FROM     Calendar_ALT
WHERE  ([Cal Mstr Date] > CONVERT(DATETIME, '2008-08-01 00:00:00', 102)) AND ([Cal Mstr Date] <= GETDATE())
ORDER BY [Cal Mstr Date]

creates the dates for the entire time period that ideally has associated Customer, Sales and Model values.

New to SQL and SSRS, your help is appreciated.


 
 
Chart with data gaps
321071
 

Answer : SQL union to fill date data series gaps for SSRS year on year chart

Create a Stored procedure to run the SSRS Report.

In the procedure
1. create a [temporary table] to hold the results your query returns above. Create default values of zero in the summary columns.
2. Insert your data into the [temporary table] with the select query you are returning data with now. Change your join to full outer join when binding to the date table.
3. Your table will create zero values for every date that is passed without numeric data.

Select the results from the temporary table to return to the SSRS Report.
Random Solutions  
 
programming4us programming4us