I have 4 tables Items, DimDate, Purchase and Sales for items definitions, calendar table, purchased and sold Items respectively. Tables structure here
I need to outer join sum(Purchased_Qty) and sum(Sales_Qty) with a date range, and also with the Items table so that the results would display as follows:
Date, Items.Product_Name, Sum(purchased_Qty), Sum(sold_Qty)
group by [Date], Items.Product_Name.
I need to display purchased & sold quantities for all items and for all days of a date range.
THIS is an example of a possible starting point query, i tried several combinations of joins & sub queries on query in this example, but none of them is correct in logic as i tested them.
Best Answer
According to this I applied Cartesian product between dimDates and Items using OUTER APPLY, that should return a combination of all dates and all items.
Then I've used two subqueries in the SELECT but you could LEFT JOIN Purchases and Sales tables.
dbfiddle here