I am a little puzzled by how I can do this.
I have a very simple query that currently returns sales for each product by year and month. It is grouped by year / month and summarizes the quantity. This returns one row for each product / year / month combo where the sale was.
If there was no sale within a month, then there is no data.
I would like my query to return one row of data for each product for each year / month in my date range, regardless of whether there was an order.
If there is no order, then I can return 0 for this product / year / month.
Below is an example of my example.
Declare @DateFrom datetime, @DateTo Datetime
Set @DateFrom = '2012-01-01'
set @DateTo = '2013-12-31'
select
Convert(CHAR(4),order_header.oh_datetime,120) + '/' + Convert(CHAR(2),order_header.oh_datetime,110) As YearMonth,
variant_detail.vad_variant_code,
sum(order_line_item.oli_qty_required) as 'TotalQty'
From
variant_Detail
join order_line_item on order_line_item.oli_vad_id = variant_detail.vad_id
join order_header on order_header.oh_id = order_line_item.oli_oh_id
Where
(order_header.oh_datetime between @DateFrom and @DateTo)
Group By
Convert(CHAR(4),order_header.oh_datetime,120) + '/' + Convert(CHAR(2),order_header.oh_datetime,110),
variant_detail.vad_variant_code