CT A(START_DATE DATE,END_DATE DATE);
INS INTO A VALUES('2013-06-03','2013-06-13');
INS INTO A VALUES('2013-06-06','2013-06-12');
INS INTO A VALUES('2013-06-05','2013-06-22');
CT B(HOL_DATE DATE, BANK_HOLIDAY CHAR(1));
INS INTO B VALUES('2013-06-01','Y');
INS INTO B VALUES('2013-06-02','Y');
INS INTO B VALUES('2013-06-09','Y');
INS INTO B VALUES('2013-06-08','Y');
INS INTO B VALUES('2013-06-15','Y');
INS INTO B VALUES('2013-06-16','Y');
INS INTO B VALUES('2013-06-22','Y');
INS INTO B VALUES('2013-06-23','Y');
select start_date, end_date,
end_date-start_date+1 as period_length,
sum(case when b.bank_holiday='Y' then 0 else 1 end) as working_days,
sum(case when b.bank_holiday='Y' then 1 else 0 end) as holiday_days
from a
join sys_calendar.CALENDAR as c
on c.calendar_date between a.start_date and a.end_date
left join b ON c.calendar_date = b.hol_date
group by 1,2
order by 1,2
CT A(START_DATE DATE,END_DATE DATE);
INS INTO A VALUES('2013-06-03','2013-06-13');
INS INTO A VALUES('2013-06-06','2013-06-12');
INS INTO A VALUES('2013-06-05','2013-06-22');
CT B(HOL_DATE DATE, BANK_HOLIDAY CHAR(1));
INS INTO B VALUES('2013-06-01','Y');
INS INTO B VALUES('2013-06-02','Y');
INS INTO B VALUES('2013-06-09','Y');
INS INTO B VALUES('2013-06-08','Y');
INS INTO B VALUES('2013-06-15','Y');
INS INTO B VALUES('2013-06-16','Y');
INS INTO B VALUES('2013-06-22','Y');
INS INTO B VALUES('2013-06-23','Y');
select start_date, end_date,
end_date-start_date+1 as period_length,
sum(case when b.bank_holiday='Y' then 0 else 1 end) as working_days,
sum(case when b.bank_holiday='Y' then 1 else 0 end) as holiday_days
from a
join sys_calendar.CALENDAR as c
on c.calendar_date between a.start_date and a.end_date
left join b ON c.calendar_date = b.hol_date
group by 1,2
order by 1,2