Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL COUNT data by date, and then COUNT by date range

$
0
0

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_DateStore_NoCodeDescriptionUnits_Sold
1/11/2013801AABBK007Shirt1
1/11/2013801AABBK007Shirt1
1/11/2013801RPKHG003Pants1
1/11/2013801RPKHG003Pants2
1/11/2013803AABBK007Shirt2
1/11/2013803ZZFFJK007Jacket1
2/11/2013801AABBK007Shirt1
2/11/2013801AABBK007Shirt1
2/11/2013801AABBK007Shirt1
2/11/2013801ZZFFJK007Jacket1
3/11/2013802RPKHG003Pants1
3/11/2013802RPKHG003Pants2
4/11/2013802ZZFFJK007Jacket1
5/11/2013802ZZFFJK007Jacket1
5/11/2013802ZZFFJK007Jacket1
5/11/2013802ZZFFJK007Jacket1
5/11/2013802ZZFFJK007Jacket1
11/11/2013803AABBK007Shirt2
1/12/2013802RPKHG003Pants1
2/12/2013803RPKHG003Pants1
1/12/2013802RPKHG003Pants2
2/12/2013803RPKHG003Pants2

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_DateStore_NoCodeDescriptionUnits_Sold
1/11/2013801AABBK007Shirt2
1/11/2013801RPKHG003Pants3
1/11/2013803AABBK007Shirt2
1/11/2013803ZZFFJK007Jacket1
2/11/2013801AABBK007Shirt3
2/11/2013801ZZFFJK007Jacket1
3/11/2013802RPKHG003Pants3
4/11/2013802ZZFFJK007Jacket1
5/11/2013802ZZFFJK007Jacket4
11/11/2013803AABBK007Shirt2
1/12/2013802RPKHG003Pants3
2/12/2013803RPKHG003Pants3

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_DateStore_NoCodeDescriptionUnits_Sold+/-1Day
1/11/2013801AABBK007Shirt23
1/11/2013801RPKHG003Pants30
1/11/2013803AABBK007Shirt20
1/11/2013803ZZFFJK007Jacket10
2/11/2013801AABBK007Shirt32
2/11/2013801ZZFFJK007Jacket10
3/11/2013802RPKHG003Pants30
4/11/2013802ZZFFJK007Jacket14
5/11/2013802ZZFFJK007Jacket41
11/11/2013803AABBK007Shirt20
1/12/2013802RPKHG003Pants30
2/12/2013803RPKHG003Pants30

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.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>