Skip to content
Advertisement

How to get data for missing weeks in Summarised data

I have two tables stores_data and financial_week as shown below. Stores data is a summarised data across multiple attributes. My task is to generate data for all the weeks present in the second table, if data is missing, the quantity should be listed as 0.

Expected Result set is this –

enter image description here

I have done cross join but after that I am not clearly sure how to proceed with getting missing weeks.

Here is the query I used so far but not seeing the null values to which I can put a logic in –

Advertisement

Answer

Use a cross join to generate the rows and then left join to bring in the values:

Note that the first part of the cross join generates all (distinct) combinations of store, supplier, and category. The final join conditions use all the columns from the cross joined tables.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement