Hello. I am using SQL Server 2008 R2. I'm trying to create a query that aggregates by 3M, 6M, 9M, 12M totals. Here is some sample data:
create table invoice (store int, invoice_date datetime, customer int, is_repeat varchar(1));
insert into invoice (store, invoice_date, customer, is_repeat)
values (1, '2015-10-05', 1, 'N'),
(1, '2016-03-04', 1, 'Y'),
(1, '2016-02-07', 1, 'Y'),
(1, '2015-08-03', 2, 'Y'),
(2, '2015-12-01', 3, 'Y'),
(2, '2016-02-16', 4, 'Y'),
(2, '2015-06-11', 3, 'Y'),
(2, '2015-09-18', 4, 'Y');
The query needs to give a 3M, 6M, etc count of 'Y' values and aggregate it by store, month, and year. For example, there should be a record for Store 1 for March 2016 that totals the number of 'Y' values between 12/1/2015 and 02/29/2016 and puts that value in the 3M column. Then also sum the values between 9/1/2015 and 02/29/2016 and put those values in the 6M column.
Here's an example of the what the query results should be for the above data:
STORE MONTH YEAR 3M_REPEATS 6M_REPEATS
----- ----- ---- ---------- ----------
1 2 2016 0 2
1 3 2016 1 2
2 2 2016 1 3
2 3 2016 2 3
I'm trying to find an elegant solution but I'm not sure if I should use a CTE, or PIVOT, or OVER(PARTITION BY), or just standard GROUP BY clauses.
I ultimately just need the last 12 months in this table, so there would only be 12 records in this table. For example, if current month/year is 3/2016, it would have 2/2015 - 3/2016 and the totals for each month.