no that won't work for non consecutive days, it is also creating a record for every 3 day period within the 3 - 6 months. Do you really want all of that or are you only interested in the MOST activity. I am assuming also that your table is not summarized and you may have multiple sales records for the same item on the same day. Here's what I did:
1. Create this table:
Sale_ID |
Item_ID |
Customer_ID |
Sale_Date |
Sale_Qty |
1 |
1 |
1 |
1/1/2014 |
1 |
2 |
1 |
2 |
1/1/2014 |
5 |
3 |
1 |
3 |
1/1/2014 |
1 |
4 |
1 |
1 |
1/5/2014 |
10 |
5 |
1 |
2 |
1/6/2014 |
2 |
6 |
1 |
2 |
1/7/2014 |
5 |
7 |
1 |
3 |
1/10/2014 |
4 |
8 |
2 |
1 |
1/1/2014 |
5 |
9 |
2 |
2 |
1/2/2014 |
10 |
10 |
2 |
3 |
1/3/2014 |
1 |
11 |
2 |
4 |
1/4/2014 |
2 |
12 |
2 |
1 |
1/5/2014 |
6 |
2. Create a table named tblDATERANGES with two fields STARTDATE and ENDDATE, make the primary key of this table a combination of startdate and enddate
3. Run this code
Code:
Dim dMinDate As Date
Dim dMaxDate As Date
Dim dCurrDate As Date
Dim dStartRange As Date
Dim dEndRange As Date
Dim sSQL As String
Dim db As Database
dMinDate = #1/1/2014#
dMaxDate = #1/31/2014#
dCurrDate = dMinDate
Set db = CurrentDb
db.Execute ("DELETE * FROM tblDateRanges")
Do While dCurrDate <= dMaxDate - 3
If DatePart("w", dCurrDate) = 7 Then
dStartRange = dCurrDate + 2
dEndRange = dCurrDate + 5
ElseIf DatePart("w", dCurrDate) = 1 Then
dStartRange = dCurrDate + 1
dEndRange = dCurrDate + 4
Else
dStartRange = dCurrDate
dEndRange = dCurrDate + 3
End If
sSQL = "INSERT INTO tblDateRanges ("
sSQL = sSQL & "StartDate, "
sSQL = sSQL & "EndDate"
sSQL = sSQL & ") VALUES ("
sSQL = sSQL & "#" & dStartRange & "#, "
sSQL = sSQL & "#" & dEndRange & "#"
sSQL = sSQL & ")"
db.Execute sSQL
dCurrDate = dCurrDate + 1
Loop
This will populate the table tblDateRanges with 'valid' date ranges that excludes weekends.
4. Run this query:
Code:
SELECT tblSales.Item_ID, tblDateRanges.StartDate, tblDateRanges.EndDate, Sum(IIf([sale_date] Between [startdate] And [enddate],[sale_qty],0)) AS 3DayNet
FROM tblDateRanges, tblSales
GROUP BY tblSales.Item_ID, tblDateRanges.StartDate, tblDateRanges.EndDate;
This query is known as a cartesian query, where you are taking every possible permuation of the start/enddate and the item_ID, I've just summarized them by the item id/startdate/enddate to give you the result you want. I perfer this method because you are processing a minimum of data rather than processing all of your sales information every time you run it. You're just re-arranging the valid sets of dates and running a query which is far, far more efficient than trying to process the sales table every time.