Hi All, hoping someone can help point me in the right direction here. I am using SQL2005 and need to perform some counts based on date ranges.
An extract of table looks as follows (real table is several 100,00's records)
(Transactions)
===============================================
Trans_Date | Store_No | Code | Description | Units_Sold |
1/11/2013 | 801 | AABBK007 | Shirt | 1 |
1/11/2013 | 801 | AABBK007 | Shirt | 1 |
1/11/2013 | 801 | RPKHG003 | Pants | 1 |
1/11/2013 | 801 | RPKHG003 | Pants | 2 |
1/11/2013 | 803 | AABBK007 | Shirt | 2 |
1/11/2013 | 803 | ZZFFJK007 | Jacket | 1 |
2/11/2013 | 801 | AABBK007 | Shirt | 1 |
2/11/2013 | 801 | AABBK007 | Shirt | 1 |
2/11/2013 | 801 | AABBK007 | Shirt | 1 |
2/11/2013 | 801 | ZZFFJK007 | Jacket | 1 |
3/11/2013 | 802 | RPKHG003 | Pants | 1 |
3/11/2013 | 802 | RPKHG003 | Pants | 2 |
4/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
11/11/2013 | 803 | AABBK007 | Shirt | 2 |
1/12/2013 | 802 | RPKHG003 | Pants | 1 |
2/12/2013 | 803 | RPKHG003 | Pants | 1 |
1/12/2013 | 802 | RPKHG003 | Pants | 2 |
2/12/2013 | 803 | RPKHG003 | Pants | 2 |
I can use this query to get the COUNT of items sold per date,store and code, no problem:
SELECT Transaction_Date, Store_No, Code, Description, COUNT(Units_Sold)
FROM Transactions
GROUP BY Transaction_Date, Store_No, Code, Description
==================================================
Transaction_Date | Store_No | Code | Description | Units_Sold |
1/11/2013 | 801 | AABBK007 | Shirt | 2 |
1/11/2013 | 801 | RPKHG003 | Pants | 3 |
1/11/2013 | 803 | AABBK007 | Shirt | 2 |
1/11/2013 | 803 | ZZFFJK007 | Jacket | 1 |
2/11/2013 | 801 | AABBK007 | Shirt | 3 |
2/11/2013 | 801 | ZZFFJK007 | Jacket | 1 |
3/11/2013 | 802 | RPKHG003 | Pants | 3 |
4/11/2013 | 802 | ZZFFJK007 | Jacket | 1 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 4 |
11/11/2013 | 803 | AABBK007 | Shirt | 2 |
1/12/2013 | 802 | RPKHG003 | Pants | 3 |
2/12/2013 | 803 | RPKHG003 | Pants | 3 |
What I need to ask for some assistance, is that I also need to add another column, that gives me a COUNT of units sold +/- 1 day either side of the transaction_date as well (but not equal to the transaction date/store/code per record returned):
Transaction_Date | Store_No | Code | Description | Units_Sold | +/-1Day |
1/11/2013 | 801 | AABBK007 | Shirt | 2 | 3 |
1/11/2013 | 801 | RPKHG003 | Pants | 3 | 0 |
1/11/2013 | 803 | AABBK007 | Shirt | 2 | 0 |
1/11/2013 | 803 | ZZFFJK007 | Jacket | 1 | 0 |
2/11/2013 | 801 | AABBK007 | Shirt | 3 | 2 |
2/11/2013 | 801 | ZZFFJK007 | Jacket | 1 | 0 |
3/11/2013 | 802 | RPKHG003 | Pants | 3 | 0 |
4/11/2013 | 802 | ZZFFJK007 | Jacket | 1 | 4 |
5/11/2013 | 802 | ZZFFJK007 | Jacket | 4 | 1 |
11/11/2013 | 803 | AABBK007 | Shirt | 2 | 0 |
1/12/2013 | 802 | RPKHG003 | Pants | 3 | 0 |
2/12/2013 | 803 | RPKHG003 | Pants | 3 | 0 |
Appreciate any assistance, I'm just trying to determine the best method for this to get started, as I'll later add some other columns for +/- 7 days and also add SUM of the sell prices as well. Just trying to trend the sales volume over time by looking at how the quantity sold moves from day to day, week to week.
Thanks, Rich.