Excluding Weekends

T

TERRY

I need to build a query that gives a production turn time
([datereceived]-[datesent]). That part is easy, but I also need to exclude
weekends so that only workdays are included in this calculation. The end
result for example being that if the date received was 05/08/06 and the date
shipped was 05/15/06, the result of my query needs to be 5.

TIA
Terry Schawe
 
G

Guest

Create a table named NCount with a number, integer, field Ncount and numbers
zero through your maximum calendar span of days.

SELECT Count(Weekday([Date open]+[Ncount])) AS Workdays
FROM [Change Requests], [NCount]
WHERE ((([Date open]+[Ncount])<=[date close]) AND ((Weekday([Date
open]+[Ncount])) Between 2 And 6));
 
G

Guest

Terry:

Two ways you can do this:

1. Create a function in a standard module, such as:

Public Function WorkDaysDiff(varLastDate, varFirstDate)

Dim lngDaysDiff As Long, lngWeekendDays As Long
Dim intPubHols As Integer

If IsNull(varLastDate) Or IsNull(varFirstDate) Then
Exit Function
End If

' if first date is Sat or Sun start on following Monday
Select Case Weekday(varFirstDate, vbMonday)
Case vbSaturday
varFirstDate = varFirstDate + 2
Case vbSunday
varFirstDate = varFirstDate + 1
End Select

' if last date is Sat or Sun finish on following Monday
Select Case Weekday(varLastDate, vbMonday)
Case vbSaturday
varLastDate = varLastDate + 2
Case vbSunday
varLastDate = varLastDate + 1
End Select

' get total date difference in days
lngDaysDiff = DateDiff("d", varFirstDate, varLastDate)

' get date difference in weeks and multiply by 2
' to get number of weekend days
lngWeekendDays = DateDiff("ww", varFirstDate, varLastDate, vbMonday) * 2

' subtract number of weekend days from total date difference
' to return number of working days
WorkDaysDiff = lngDaysDiff - lngWeekendDays

End Function

and call it in the query:

WorkDaysDiff([datereceived], [datesent])

2. Create a Calendar table, which is simply a table of all dates over a
given period. An easy way is to fill a column in Excel and import it into
Access. You can then simply count the days in the table between two dates
excluding the weekends, i.e.

WHERE Weekday(calDate, 2) < 6.

Ken Sheridan
Stafford, England
 
T

TERRY

The table itself holds all of our sales data, but the only feilds in this
particular query would be [faileno] [appraiserid] [datereceived] and
[datesent] the first two being text feilds and the second two being date
feilds.
KARL DEWEY said:
What is your table structure?

TERRY said:
I need to build a query that gives a production turn time
([datereceived]-[datesent]). That part is easy, but I also need to
exclude
weekends so that only workdays are included in this calculation. The end
result for example being that if the date received was 05/08/06 and the
date
shipped was 05/15/06, the result of my query needs to be 5.

TIA
Terry Schawe
 
G

Guest

Try this --
SELECT [Sales Data].faileno, [Sales Data].appraiserid,
Count((Weekday([datereceived]+[Ncount]))) AS Workdays
FROM [Sales Data], NCount
WHERE ((([datereceived]+[Ncount])<=[datesent]) AND
((Weekday([datereceived]+[Ncount])) Between 2 And 6))
GROUP BY [Sales Data].faileno, [Sales Data].appraiserid;


TERRY said:
The table itself holds all of our sales data, but the only feilds in this
particular query would be [faileno] [appraiserid] [datereceived] and
[datesent] the first two being text feilds and the second two being date
feilds.
KARL DEWEY said:
What is your table structure?

TERRY said:
I need to build a query that gives a production turn time
([datereceived]-[datesent]). That part is easy, but I also need to
exclude
weekends so that only workdays are included in this calculation. The end
result for example being that if the date received was 05/08/06 and the
date
shipped was 05/15/06, the result of my query needs to be 5.

TIA
Terry Schawe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top