So, let us say the problem is to calculate business days count which is defined as count of days (optionally inclusive in the current implementation) excluding weekend days and holidays.
Let us say periods to calculate are stored in table associated with contacts.
[tblPeriods]
keyPeriodID - Autonumber(Long), PK
keyContactID - Long, FK(tblContacts)
dteStart - Date/Time
dteEnd - Date/Time
The goal is to receive dataset containing sequential dates falling into date periods stored in the table with weekends and holidays excluded. Then, a simple grouping query will return desired results.
Step 1. Getting records.
To get all days falling into the periods we need to outer join [tblPeriods] with dataset containing all sequential dates.
Obviously using a table storing all dates is not a smart option. ;)
Better to generate it dynamically.
Obviously calendar dates are nothing more than all possible combinations of 1-31 days numbers, 1-12 month numbers and sensible range of year numbers. Certainly non-existing dates like 30-Dec and sometimes 29-Dec has to be omitted.
This gives an idea to use cartesian join of the following datasets:
[tblDays]
lngDay - Long, PK - natural numbers set 1-31
[tblMonts]
lngMonth - Long, PK - natural numbers set 1-12
[tblYears]
lngYear - Long, PK - natural numbers set ... :) ... let us say 2000 - 2014
The following query will combine these values into flat calendar 2000-2014, non existing dates are excluded using a feature of DateSerial() function to wrap day in a case of illegal argumnets.
Query: [qryFlatCalendar]
Expand|Select|Wrap|Line Numbers
- SELECT DateSerial(tblYears.lngYear,tblMonths.lngMonth,tblDays.lngDay) AS dteDate,
- tblYears.lngYear, tblMonths.lngMonth, tblDays.lngDay
- FROM tblYears, tblMonths, tblDays
- WHERE tblDays.lngDay=Day(DateSerial([tblYears].[lngYear],[tblMonths].[lngMonth],[tblDays].[lngDay]));
Query: [qryContactsPeriodsDays]
Expand|Select|Wrap|Line Numbers
- SELECT qryFlatCalendar.*, tblPeriods.keyPeriodID, tblPeriods.keyContactID, tblPeriods.dteStart, tblPeriods.dteEnd
- FROM qryFlatCalendar LEFT JOIN tblPeriods ON (tblPeriods.dteStart<=qryFlatCalendar.dteDate) AND (tblPeriods.dteEnd>=qryFlatCalendar.dteDate)
- WHERE Not tblPeriods.keyContactID Is Null;
Step 2. Excluding weekends and holidays.
Now we are going to exclude weekend days off and holidays.
And immediately appear two issues - as usual one is easy and other not so :)
- Weekend days off depend on particular country.
- Holidays sets depend on country too. The problem is that in some cases holiday date is the same in each year, in some cases it is calculated using some rules which not always could be easily embedded into relational database. So, for simplicity, let us store explicit full dates of that "irregular" holidays while "regular" holydays don't require more than a single record with Null year value.
This requires several tables:
[tblCountries]
keyCountryID - Autonumber(Long), PK
txtCountry - Text
[tblCountryDaysOff]
keyContactDayOffID - Autonumber(Long), PK
keyCountryID - Long, FK(tblCountries)
lngContactDayOff - Long
[tblCountryHolidays]
keyCountryHolidayID - Autonumber(Long), PK
keyCountryID - Long, FK(tblCountries)
txtHolidayName - Text
[tblContacts]
keyContactID - Autonumber(Long), PK
keyCountryID - Long, FK(tblCountries)
txtContactName - Text
* the following table associates holidays with dates, if holiday occurs in definite day of month each year, then a single record is used with [lngYear]=Null, otherwise record for each year has to be created
[tblHolydayDates]
keyHolidayID - Autonumber(Long), PK
lngDay- long
lngMonth - Long
lngYear - Long
keyCountryHolidayID - Long, FK(tblCountryHolidays)
Now two prejoins to get associations - Contact/DaysOff, Contact/HolidayDate
Query: [qryContactsDaysOff]
Expand|Select|Wrap|Line Numbers
- SELECT tblContacts.*, tblCountryDaysOff.lngContactDayOff
- FROM tblContacts INNER JOIN tblCountryDaysOff
- ON tblContacts.keyCountryID = tblCountryDaysOff.keyCountryID;
Expand|Select|Wrap|Line Numbers
- SELECT tblContacts.*, tblCountryHolidays.keyCountryHolidayID,
- tblCountryHolidays.txtHolidayName, tblHolydayDates.keyHolidayID, tblHolydayDates.lngDay, tblHolydayDates.lngMonth, tblHolydayDates.lngYear
- FROM (tblContacts INNER JOIN tblCountryHolidays
- ON tblContacts.keyCountryID = tblCountryHolidays.keyCountryID)
- INNER JOIN tblHolydayDates
- ON tblCountryHolidays.keyCountryHolidayID=tblHolydayDates.keyCountryHolidayID;
Query: [qryContactsPeriodsDaysWODaysOff]
Expand|Select|Wrap|Line Numbers
- SELECT qryContactsPeriodsDays.*, qryContactsDaysOff.lngContactDayOff
- FROM qryContactsPeriodsDays
- LEFT JOIN qryContactsDaysOff
- ON
- (qryContactsPeriodsDays.keyContactID=qryContactsDaysOff.keyContactID) AND (WeekDay(qryContactsPeriodsDays.dteDate)=qryContactsDaysOff.lngContactDayOff)
- WHERE qryContactsDaysOff.lngContactDayOff Is Null;
Query: [qryContactsPeriodsBusinessDays]
Expand|Select|Wrap|Line Numbers
- SELECT qryContactsPeriodsDaysWODaysOff.keyPeriodID,
- qryContactsPeriodsDaysWODaysOff.keyContactID, qryContactsPeriodsDaysWODaysOff.dteDate,
- qryContactsHolidaysDates.txtHolidayName
- FROM qryContactsPeriodsDaysWODaysOff
- LEFT JOIN qryContactsHolidaysDates
- ON
- (qryContactsPeriodsDaysWODaysOff.lngYear=qryContactsHolidaysDates.lngYear Or qryContactsHolidaysDates.lngYear Is Null)
- AND (qryContactsPeriodsDaysWODaysOff.lngMonth=qryContactsHolidaysDates.lngMonth) AND
- (qryContactsPeriodsDaysWODaysOff.lngDay=qryContactsHolidaysDates.lngDay) AND
- (qryContactsPeriodsDaysWODaysOff.keyContactID=qryContactsHolidaysDates.keyContactID)
- WHERE qryContactsHolidaysDates.keyCountryHolidayID Is Null;
Step 3. Counting days.
Now dessert.
Query: [qryContactsPeroidsBusinessDaysCounts]
Expand|Select|Wrap|Line Numbers
- SELECT qryContactsPeriodsBusinessDays.keyPeriodID,
- qryContactsPeriodsBusinessDays.keyContactID,
- Count(qryContactsPeriodsBusinessDays.dteDate)
- AS CountOfdteDate
- FROM qryContactsPeriodsBusinessDays
- GROUP BY qryContactsPeriodsBusinessDays.keyPeriodID,
- qryContactsPeriodsBusinessDays.keyContactID;
However periods having zero count of business days do not appear in the resulting list.
So, let Uroboros bite his tail.
Query: [qryFinalJoin]
Expand|Select|Wrap|Line Numbers
- SELECT tblPeriods.*, Nz(qryContactsPeroidsBusinessDaysCounts.CountOfdteDate,0) AS
- lngBusinessDays
- FROM tblPeriods
- LEFT JOIN qryContactsPeroidsBusinessDaysCounts
- ON tblPeriods.keyPeriodID=qryContactsPeroidsBusinessDaysCounts.keyPeriodID;
P.P.S. Ok. No sample today because the maximum allowed size was 5k. :D Just another bug in current state of bytes.com.