hi all i have a table with sample data as shown in the table below. for each month end date, I need to aggregate (sum) all the quantities for each product and customer and include the current month plus the past 12 months (including current month).
for example, for customer C123, P123 and for 7/31/2015, the query should add 7/31/2015 quantity and the quantity for 1/31/2015.
DECLARE @TEMP TABLE (Customer VARCHAR (10), Product VARCHAR(10), Month_end_date DATE, Quantity INT) INSERT INTO @TEMP VALUES ('C123', 'P123', '1/31/2015', 10) INSERT INTO @TEMP VALUES ('C124', 'P124', '2/28/2015', 20) INSERT INTO @TEMP VALUES ('C125', 'P125', '3/31/2015', 30) INSERT INTO @TEMP VALUES ('C126', 'P126', '4/30/2015', 40) INSERT INTO @TEMP VALUES ('C127', 'P127', '5/31/2015', 50) INSERT INTO @TEMP VALUES ('C128', 'P128', '6/30/2015', 60) INSERT INTO @TEMP VALUES ('C123', 'P123', '7/31/2015', 70) INSERT INTO @TEMP VALUES ('C124', 'P124', '8/31/2015', 80) INSERT INTO @TEMP VALUES ('C125', 'P125', '9/30/2015', 90) INSERT INTO @TEMP VALUES ('C126', 'P126', '10/31/2015', 20) INSERT INTO @TEMP VALUES ('C127', 'P127', '11/30/2015', 10) INSERT INTO @TEMP VALUES ('C128', 'P128', '12/31/2015', 40)
the output for the above example should be
C123 P123 7/31/2015 70+10=80
is this possible?
- Edited by PeaceOut 6 hours 1 minutes ago