sql server 2008 - "neater" way of counting holidays -
i have neat table of holidays showing date , recurrence if such holiday recurrent (i.e. new year)
now, need count number of holidays between 2 dates. if simple list of dates without info recurrence (so i.e. show new years between 2000-01-01 , 2015-01-01) quite easy, i.e. like
declare @start_date date= '2013-01-02', @end_date date ='2014-01-02' select count(ce.name) holidays_count dbo.argo_cal_event ce inner join dbo.argo_cal_event_type cet on ce.event_type_gkey = cet.gkey (cet.name = 'exempt_day') , (ce.name <> 'sundays') , (ce.occ_start between @start_date , @end_date)
but have neat recurrence, query above won't count new years, christmases etc have been declared happening "every year starting from".
i could create table such list, i've been wondering, there other way?
edit: let me precise had in mind: i'd count event if event occurs once (i assume here user have populate irregular holidays i.e. easter), when recurrence <> once, occurrence start , count years between date , final date.
edit2: think i've got - recurrent holidays can use
select sum (datediff (year, ce.occ_start, @end_date)) recurrent_holidays dbo.argo_cal_event ce inner join dbo.argo_cal_event_type cet on ce.event_type_gkey = cet.gkey (cet.name = 'exempt_day') , (ce.repeat_interval ='annually')
edit3: unfortunately solution doesn't work (or @ least getting quite complicated) if i'd count between 2 dates, in 1 taken table, i.e if i'd count recurring holidays between unit.time_in , getddate() :/
the straightforward way make them one-time occurrences.
the table isn't going that big can't add in every sunday. it's 52 or 53 entries per year.
if way can like,
select count(*) events event_date between start_date , end_date
done.
the main reason way, though, of holidays tricky calculate. you'll need calculating (or looking up) dates friday, easter monday, , others. they're based on phase of moon.
why not calculate them , make query easy?
in end, matters lot less how "neat" data structures , lot more on whether code it's supposed do, how long takes it, , how effort takes working.
Comments
Post a Comment