Submitted by brians on

It's Friday, and the boss just asked you to produce a report that summarizes all the issues your team has dealt with in the past year, along with a chart that indicates at a glance the average number of business days that passed between the date an issue was reported to the date it was closed. No problem, right? But how can you reliably count only actual business days without including weekends or holidays? And to make matters worse, your company has a policy of giving employees Fridays off if a holiday falls on a Saturday, and Mondays off if one falls on a Sunday. You also get to take the Friday after Thanksgiving as a holiday. What to do?

This sort of situation is the perfect scenario for a calendar table to swoop in and save the day. I've worked on several applications which made use of a calendar table, and I adapted the concepts at this site and another one to suit my needs.

What is a calendar table? In its most basic form, a calendar table is simply a database table that contains one record for every date in a specified range and a date field to store each individual date. Additional fields can store values to quickly determine the year, month, day, day of the week, day of the year or week number, or to indicate whether a date is a weekend, a holiday, a payday, or whatever characteristic you want to assign to a given day. Thus, because a calendar table can explicitly identify which dates are actual business days, a simple SQL query can easily provide you with the necessary data to satisfy your boss's reporting requirements.

I'm going to explain how to build a calendar table with the MySQL database, but the basic principles should be easily applicable to other databases as well.

The first thing you need to do before building a calendar table is to determine the range of dates you want to store in it and then calculate the total number of records the table will hold. Assuming you've connected to your MySQL server with the mysql client, the following command will produce the number of days between the ending and starting date:

[geshifilter-sql] select datediff('2040-12-31','2010-01-01'); [/geshifilter-sql]This produces a count of 11.322 days.

Now let's create the actual calendar table. We'll need not only dates, but also various fields that can be used to set additional data associated with each date, such as holidays, weekends, paydays, etc.

[geshifilter-sql] create table calendar_table ( dt date not null primary key, y smallint null, q tinyint null, m tinyint null, d tinyint null, dw tinyint null, monthName varchar(9) null, dayName varchar(9) null, w tinyint null, isWeekday binary(1) null, isHoliday binary(1) null, holidayDescr varchar(32) null, isPayday binary(1) null ); [/geshifilter-sql]After that's done, we need to populate that new table with one record for each date in the desired range. Rather than write a program to insert all 11,322 records one by one, we can use MySQL to create data right out of thin air! If you need more dates than can be obtained with this code, simply add more joins and calculations to obtain the desired number.

[geshifilter-sql] create table ints ( i tinyint ); insert into ints values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); insert into calendar_table (dt) select date('2010-01-01') + interval a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i day from ints a join ints b join ints c join ints d join ints e where (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) <= 11322 order by 1; [/geshifilter-sql]Now that the table is populated with dates, we're ready to set the other fields to appropriate values. The following SQL will mark which dates are weekends as well as fill in the year, month, day, day of the week, quarter, month name, day name and week number fields.

[geshifilter-sql] update calendar_table set isWeekday = case when dayofweek(dt) in (1,7) then 0 else 1 end, isHoliday = 0, isPayday = 0, y = year(dt), q = quarter(dt), m = month(dt), d = dayofmonth(dt), dw = dayofweek(dt), monthname = monthname(dt), dayname = dayname(dt), w = week(dt), holidayDescr = ''; [/geshifilter-sql]The following SQL sets the New Year's Day holiday and handles situations where a holiday falls on a weekend.

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'New Year''s Day' where m = 1 and d = 1; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt + interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for New Year''s Day' where c1.dw = 6 and c2.m = 1 and c2.dw = 7 and c2.isHoliday = 1; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt - interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for New Year''s Day' where c1.dw = 2 and c2.m = 1 and c2.dw = 1 and c2.isHoliday = 1; [/geshifilter-sql]Set the Martin Luther King holiday (third Monday in January).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Martin Luther King Day' where m = 1 and dw = 2 and d between 15 and 21; [/geshifilter-sql]Set the President's Day holiday (third Monday in February).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'President''s Day' where m = 2 and dw = 2 and d between 15 and 21; [/geshifilter-sql]Set the Memorial Day holiday (last Monday in May).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Memorial Day' where m = 5 and dw = 2 and d between 25 and 31; [/geshifilter-sql]Set the Independence Day holiday and handle situations where a holiday falls on a weekend.

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Independence Day' where m = 7 and d = 4; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt + interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Independence Day' where c1.dw = 6 and c2.m = 7 and c2.d = 4 and c2.dw = 7 and c2.isHoliday = 1; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt - interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Independence Day' where c1.dw = 2 and c2.m = 7 and c2.d = 4 and c2.dw = 1 and c2.isHoliday = 1; [/geshifilter-sql]Set the Labor Day holiday (first Monday in September).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Labor Day' where m = 9 and dw = 2 and d between 1 and 7; [/geshifilter-sql]Set the Veteran's Day holiday and handle situations where the holiday falls on a weekend.

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Veteran''s Day' where m = 11 and d = 11; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt + interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Veteran''s Day' where c1.dw = 6 and c2.m = 11 and c2.d = 11 and c2.dw = 7 and c2.isHoliday = 1; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt - interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Veteran''s Day' where c1.dw = 2 and c2.m = 11 and c2.d = 11 and c2.dw = 1 and c2.isHoliday = 1; [/geshifilter-sql]Set the Thanksgiving Day holiday (fourth Thursday in November).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Thanksgiving Day' where m = 11 and dw = 5 and d between 22 and 28; [/geshifilter-sql]Set a Black Friday holiday if desired (day after Thanksgiving, or the fourth Friday in November).

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Friday After Thanksgiving' where m = 11 and dw = 6 and d between 21 and 29; [/geshifilter-sql]Set the Christmas Day holiday and handle situations where the holiday falls on a weekend.

[geshifilter-sql] update calendar_table set isHoliday = 1, holidayDescr = 'Christmas Day' where m = 12 and d = 25; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt + interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Christmas Day' where c1.dw = 6 and c2.m = 12 and c2.d = 25 and c2.dw = 7 and c2.isHoliday = 1; update calendar_table c1 left join calendar_table c2 on c2.dt = c1.dt - interval 1 day set c1.isHoliday = 1, c1.holidayDescr = 'Holiday for Christmas Day' where c1.dw = 2 and c2.m = 12 and c2.d = 25 and c2.dw = 1 and c2.isHoliday = 1; [/geshifilter-sql]Set paydays. Begin with a specified date, then calculate paydays before and after that date up to the beginning and ending dates of the range specified earlier when we calculated the number of records the calendar table would need to have. In this case, paydays are every other Friday, and the payday we will use to begin the calculations is on 3/25/2011.

[geshifilter-sql] update calendar_table set isPayday = 1 where dt in ( select date('2011-03-25') - interval (a.i*100 + b.i*10 + c.i) * 2 week from ints a join ints b join ints c where date('2011-03-25') - interval (a.i*100 + b.i*10 + c.i) * 2 week >= date('2010-01-01') union select date('2011-03-25') + interval (a.i*100 + b.i*10 + c.i) * 2 week from ints a join ints b join ints c where date('2011-03-25') + interval (a.i*100 + b.i*10 + c.i) * 2 week <= date('2040-12-31') ); [/geshifilter-sql]We have a problem though. Some of those paydays fall on holidays, so we need to move the actual payday to the day before the holiday. The following SQL will accomplish this.

[geshifilter-sql] update calendar_table set isPayday = 1 where dt in ( select newDt from ( select dt - interval 1 day as newDt from calendar_table where isPayday = 1 and isHoliday = 1 and dt > '2010-01-01' ) as x ); update calendar_table set isPayday = 0 where isPayday = 1 and isHoliday = 1; [/geshifilter-sql]Now our calendar table is complete and ready for use. We can use a SQL command something like the following to obtain the number of actual business days that elapsed between the date an issue was opened to when it was closed. The final result is a report that impresses the boss by delivering statistics with pinpoint accuracy.

[geshifilter-sql] select workorder_nbr, descr, open_dt, close_dt, (select count(*) from calendar_table where dt > open_dt and dt <= close_dt and isWeekday = 1 and isHoliday = 0 ) as work_days from workorders; [/geshifilter-sql]