我有主日期表的工作日期,该日期有3列
a。 wd:(type int)该列具有int格式的所有工作日期,用于 一年不包括周末和节假日。
b。 fulldate(数据类型为date):与wd相同,但采用日期格式。
c。 dayname:其中包含工作日名称,例如星期一,星期二。
我想按照以下规则为每个月生成一个包含两个日期(两列)的列表。
a。每个月的第二个星期五(如果第二个星期五是该月的假期,那么workDates.wd中将没有该日期,在这种情况下,我需要以前的工作,即:表中可用的星期四或星期三)>
b。每个月的第三个星期五之后的星期一(在这种情况下,如果星期一是假期,我需要下一个工作日,即表中可用的工作日为星期二或星期三)
在这种情况下,我需要您的帮助来创建查询。
预期输出:
year month rule_1_dates rule_2_dates
2019 Jan 20190111 20190121
等等……。
基本表workingDates数据:
declare @workingDates table (wd int , fulldate date , [dayname] varchar(30))
insert into @workingDates values (20190102,’2019-01-02′,’Wednesday’)
insert into @workingDates values (20190103,’2019-01-03′,’Thursday’)
insert into @workingDates values (20190104,’2019-01-04′,’Friday’)
insert into @workingDates values (20190107,’2019-01-07′,’Monday’)
insert into @workingDates values (20190108,’2019-01-08′,’Tuesday’)
insert into @workingDates values (20190109,’2019-01-09′,’Wednesday’)
insert into @workingDates values (20190110,’2019-01-10′,’Thursday’)
insert into @workingDates values (20190111,’2019-01-11′,’Friday’)
insert into @workingDates values (20190114,’2019-01-14′,’Monday’)
insert into @workingDates values (20190115,’2019-01-15′,’Tuesday’)
insert into @workingDates values (20190116,’2019-01-16′,’Wednesday’)
insert into @workingDates values (20190117,’2019-01-17′,’Thursday’)
insert into @workingDates values (20190118,’2019-01-18′,’Friday’)
insert into @workingDates values (20190121,’2019-01-21′,’Monday’)
insert into @workingDates values (20190122,’2019-01-22′,’Tuesday’)
insert into @workingDates values (20190123,’2019-01-23′,’Wednesday’)
insert into @workingDates values (20190124,’2019-01-24′,’Thursday’)
insert into @workingDates values (20190125,’2019-01-25′,’Friday’)
insert into @workingDates values (20190128,’2019-01-28′,’Monday’)
insert into @workingDates values (20190129,’2019-01-29′,’Tuesday’)
insert into @workingDates values (20190130,’2019-01-30′,’Wednesday’)
insert into @workingDates values (20190131,’2019-01-31′,’Thursday’)
insert into @workingDates values (20190201,’2019-02-01′,’Friday’)
insert into @workingDates values (20190204,’2019-02-04′,’Monday’)
insert into @workingDates values (20190205,’2019-02-05′,’Tuesday’)
insert into @workingDates values (20190206,’2019-02-06′,’Wednesday’)
insert into @workingDates values (20190207,’2019-02-07′,’Thursday’)
insert into @workingDates values (20190208,’2019-02-08′,’Friday’)
insert into @workingDates values (20190211,’2019-02-11′,’Monday’)
insert into @workingDates values (20190212,’2019-02-12′,’Tuesday’)
insert into @workingDates values (20190213,’2019-02-13′,’Wednesday’)
insert into @workingDates values (20190214,’2019-02-14′,’Thursday’)
insert into @workingDates values (20190215,’2019-02-15′,’Friday’)
insert into @workingDates values (20190218,’2019-02-18′,’Monday’)
insert into @workingDates values (20190219,’2019-02-19′,’Tuesday’)
insert into @workingDates values (20190220,’2019-02-20′,’Wednesday’)
insert into @workingDates values (20190221,’2019-02-21′,’Thursday’)
insert into @workingDates values (20190222,’2019-02-22′,’Friday’)
insert into @workingDates values (20190225,’2019-02-25′,’Monday’)
insert into @workingDates values (20190226,’2019-02-26′,’Tuesday’)
insert into @workingDates values (20190227,’2019-02-27′,’Wednesday’)
insert into @workingDates values (20190228,’2019-02-28′,’Thursday’)
insert into @workingDates values (20190301,’2019-03-01′,’Friday’)
insert into @workingDates values (20190304,’2019-03-04′,’Monday’)
insert into @workingDates values (20190305,’2019-03-05′,’Tuesday’)
insert into @workingDates values (20190306,’2019-03-06′,’Wednesday’)
insert into @workingDates values (20190307,’2019-03-07′,’Thursday’)
insert into @workingDates values (20190308,’2019-03-08′,’Friday’)
insert into @workingDates values (20190311,’2019-03-11′,’Monday’)
insert into @workingDates values (20190312,’2019-03-12′,’Tuesday’)
insert into @workingDates values (20190313,’2019-03-13′,’Wednesday’)
insert into @workingDates values (20190314,’2019-03-14′,’Thursday’)
insert into @workingDates values (20190315,’2019-03-15′,’Friday’)
insert into @workingDates values (20190318,’2019-03-18′,’Monday’)
insert into @workingDates values (20190319,’2019-03-19′,’Tuesday’)
insert into @workingDates values (20190320,’2019-03-20′,’Wednesday’)
insert into @workingDates values (20190321,’2019-03-21′,’Thursday’)
insert into @workingDates values (20190322,’2019-03-22′,’Friday’)
insert into @workingDates values (20190325,’2019-03-25′,’Monday’)
insert into @workingDates values (20190326,’2019-03-26′,’Tuesday’)
insert into @workingDates values (20190327,’2019-03-27′,’Wednesday’)
insert into @workingDates values (20190328,’2019-03-28′,’Thursday’)
insert into @workingDates values (20190329,’2019-03-29′,’Friday’)
insert into @workingDates values (20190401,’2019-04-01′,’Monday’)
insert into @workingDates values (20190402,’2019-04-02′,’Tuesday’)
insert into @workingDates values (20190403,’2019-04-03′,’Wednesday’)
insert into @workingDates values (20190404,’2019-04-04′,’Thursday’)
insert into @workingDates values (20190405,’2019-04-05′,’Friday’)
insert into @workingDates values (20190408,’2019-04-08′,’Monday’)
insert into @workingDates values (20190409,’2019-04-09′,’Tuesday’)
insert into @workingDates values (20190410,’2019-04-10′,’Wednesday’)
insert into @workingDates values (20190411,’2019-04-11′,’Thursday’)
insert into @workingDates values (20190412,’2019-04-12′,’Friday’)
insert into @workingDates values (20190415,’2019-04-15′,’Monday’)
insert into @workingDates values (20190416,’2019-04-16′,’Tuesday’)
insert into @workingDates values (20190417,’2019-04-17′,’Wednesday’)
insert into @workingDates values (20190418,’2019-04-18′,’Thursday’)
insert into @workingDates values (20190423,’2019-04-23′,’Tuesday’)
insert into @workingDates values (20190424,’2019-04-24′,’Wednesday’)
insert into @workingDates values (20190425,’2019-04-25′,’Thursday’)
insert into @workingDates values (20190426,’2019-04-26′,’Friday’)
insert into @workingDates values (20190429,’2019-04-29′,’Monday’)
insert into @workingDates values (20190430,’2019-04-30′,’Tuesday’)
insert into @workingDates values (20190501,’2019-05-01′,’Wednesday’)
insert into @workingDates values (20190502,’2019-05-02′,’Thursday’)
insert into @workingDates values (20190503,’2019-05-03′,’Friday’)
insert into @workingDates values (20190506,’2019-05-06′,’Monday’)
insert into @workingDates values (20190507,’2019-05-07′,’Tuesday’)
insert into @workingDates values (20190508,’2019-05-08′,’Wednesday’)
insert into @workingDates values (20190509,’2019-05-09′,’Thursday’)
insert into @workingDates values (20190510,’2019-05-10′,’Friday’)
insert into @workingDates values (20190513,’2019-05-13′,’Monday’)
insert into @workingDates values (20190514,’2019-05-14′,’Tuesday’)
insert into @workingDates values (20190515,’2019-05-15′,’Wednesday’)
insert into @workingDates values (20190516,’2019-05-16′,’Thursday’)
insert into @workingDates values (20190517,’2019-05-17′,’Friday’)
insert into @workingDates values (20190520,’2019-05-20′,’Monday’)
insert into @workingDates values (20190521,’2019-05-21′,’Tuesday’)
insert into @workingDates values (20190522,’2019-05-22′,’Wednesday’)
insert into @workingDates values (20190523,’2019-05-23′,’Thursday’)
insert into @workingDates values (20190524,’2019-05-24′,’Friday’)
insert into @workingDates values (20190527,’2019-05-27′,’Monday’)
insert into @workingDates values (20190528,’2019-05-28′,’Tuesday’)
insert into @workingDates values (20190529,’2019-05-29′,’Wednesday’)
insert into @workingDates values (20190530,’2019-05-30′,’Thursday’)
insert into @workingDates values (20190531,’2019-05-31′,’Friday’)
insert into @workingDates values (20190603,’2019-06-03′,’Monday’)
insert into @workingDates values (20190604,’2019-06-04′,’Tuesday’)
insert into @workingDates values (20190605,’2019-06-05′,’Wednesday’)
insert into @workingDates values (20190606,’2019-06-06′,’Thursday’)
insert into @workingDates values (20190607,’2019-06-07′,’Friday’)
insert into @workingDates values (20190610,’2019-06-10′,’Monday’)
insert into @workingDates values (20190611,’2019-06-11′,’Tuesday’)
insert into @workingDates values (20190612,’2019-06-12′,’Wednesday’)
insert into @workingDates values (20190613,’2019-06-13′,’Thursday’)
insert into @workingDates values (20190614,’2019-06-14′,’Friday’)
insert into @workingDates values (20190617,’2019-06-17′,’Monday’)
insert into @workingDates values (20190618,’2019-06-18′,’Tuesday’)
insert into @workingDates values (20190619,’2019-06-19′,’Wednesday’)
insert into @workingDates values (20190620,’2019-06-20′,’Thursday’)
insert into @workingDates values (20190621,’2019-06-21′,’Friday’)
insert into @workingDates values (20190624,’2019-06-24′,’Monday’)
insert into @workingDates values (20190625,’2019-06-25′,’Tuesday’)
insert into @workingDates values (20190626,’2019-06-26′,’Wednesday’)
insert into @workingDates values (20190627,’2019-06-27′,’Thursday’)
insert into @workingDates values (20190628,’2019-06-28′,’Friday’)
insert into @workingDates values (20190701,’2019-07-01′,’Monday’)
insert into @workingDates values (20190702,’2019-07-02′,’Tuesday’)
insert into @workingDates values (20190703,’2019-07-03′,’Wednesday’)
insert into @workingDates values (20190704,’2019-07-04′,’Thursday’)
insert into @workingDates values (20190705,’2019-07-05′,’Friday’)
insert into @workingDates values (20190708,’2019-07-08′,’Monday’)
insert into @workingDates values (20190709,’2019-07-09′,’Tuesday’)
insert into @workingDates values (20190710,’2019-07-10′,’Wednesday’)
insert into @workingDates values (20190711,’2019-07-11′,’Thursday’)
insert into @workingDates values (20190712,’2019-07-12′,’Friday’)
insert into @workingDates values (20190715,’2019-07-15′,’Monday’)
insert into @workingDates values (20190716,’2019-07-16′,’Tuesday’)
insert into @workingDates values (20190717,’2019-07-17′,’Wednesday’)
insert into @workingDates values (20190718,’2019-07-18′,’Thursday’)
insert into @workingDates values (20190719,’2019-07-19′,’Friday’)
insert into @workingDates values (20190722,’2019-07-22′,’Monday’)
insert into @workingDates values (20190723,’2019-07-23′,’Tuesday’)
insert into @workingDates values (20190724,’2019-07-24′,’Wednesday’)
insert into @workingDates values (20190725,’2019-07-25′,’Thursday’)
insert into @workingDates values (20190726,’2019-07-26′,’Friday’)
insert into @workingDates values (20190729,’2019-07-29′,’Monday’)
insert into @workingDates values (20190730,’2019-07-30′,’Tuesday’)
insert into @workingDates values (20190731,’2019-07-31′,’Wednesday’)
insert into @workingDates values (20190801,’2019-08-01′,’Thursday’)
insert into @workingDates values (20190802,’2019-08-02′,’Friday’)
insert into @workingDates values (20190805,’2019-08-05′,’Monday’)
insert into @workingDates values (20190806,’2019-08-06′,’Tuesday’)
insert into @workingDates values (20190807,’2019-08-07′,’Wednesday’)
insert into @workingDates values (20190808,’2019-08-08′,’Thursday’)
insert into @workingDates values (20190809,’2019-08-09′,’Friday’)
insert into @workingDates values (20190812,’2019-08-12′,’Monday’)
insert into @workingDates values (20190813,’2019-08-13′,’Tuesday’)
insert into @workingDates values (20190814,’2019-08-14′,’Wednesday’)
insert into @workingDates values (20190815,’2019-08-15′,’Thursday’)
insert into @workingDates values (20190816,’2019-08-16′,’Friday’)
insert into @workingDates values (20190819,’2019-08-19′,’Monday’)
insert into @workingDates values (20190820,’2019-08-20′,’Tuesday’)
insert into @workingDates values (20190821,’2019-08-21′,’Wednesday’)
insert into @workingDates values (20190822,’2019-08-22′,’Thursday’)
insert into @workingDates values (20190823,’2019-08-23′,’Friday’)
insert into @workingDates values (20190826,’2019-08-26′,’Monday’)
insert into @workingDates values (20190827,’2019-08-27′,’Tuesday’)
insert into @workingDates values (20190828,’2019-08-28′,’Wednesday’)
insert into @workingDates values (20190829,’2019-08-29′,’Thursday’)
insert into @workingDates values (20190830,’2019-08-30′,’Friday’)
insert into @workingDates values (20190902,’2019-09-02′,’Monday’)
insert into @workingDates values (20190903,’2019-09-03′,’Tuesday’)
insert into @workingDates values (20190904,’2019-09-04′,’Wednesday’)
insert into @workingDates values (20190905,’2019-09-05′,’Thursday’)
insert into @workingDates values (20190906,’2019-09-06′,’Friday’)
insert into @workingDates values (20190909,’2019-09-09′,’Monday’)
insert into @workingDates values (20190910,’2019-09-10′,’Tuesday’)
insert into @workingDates values (20190911,’2019-09-11′,’Wednesday’)
insert into @workingDates values (20190912,’2019-09-12′,’Thursday’)
insert into @workingDates values (20190913,’2019-09-13′,’Friday’)
insert into @workingDates values (20190916,’2019-09-16′,’Monday’)
insert into @workingDates values (20190917,’2019-09-17′,’Tuesday’)
insert into @workingDates values (20190918,’2019-09-18′,’Wednesday’)
insert into @workingDates values (20190919,’2019-09-19′,’Thursday’)
insert into @workingDates values (20190920,’2019-09-20′,’Friday’)
insert into @workingDates values (20190923,’2019-09-23′,’Monday’)
insert into @workingDates values (20190924,’2019-09-24′,’Tuesday’)
insert into @workingDates values (20190925,’2019-09-25′,’Wednesday’)
insert into @workingDates values (20190926,’2019-09-26′,’Thursday’)
insert into @workingDates values (20190927,’2019-09-27′,’Friday’)
insert into @workingDates values (20190930,’2019-09-30′,’Monday’)
insert into @workingDates values (20191001,’2019-10-01′,’Tuesday’)
insert into @workingDates values (20191002,’2019-10-02′,’Wednesday’)
insert into @workingDates values (20191003,’2019-10-03′,’Thursday’)
insert into @workingDates values (20191004,’2019-10-04′,’Friday’)
insert into @workingDates values (20191007,’2019-10-07′,’Monday’)
insert into @workingDates values (20191008,’2019-10-08′,’Tuesday’)
insert into @workingDates values (20191009,’2019-10-09′,’Wednesday’)
insert into @workingDates values (20191010,’2019-10-10′,’Thursday’)
insert into @workingDates values (20191011,’2019-10-11′,’Friday’)
insert into @workingDates values (20191014,’2019-10-14′,’Monday’)
insert into @workingDates values (20191015,’2019-10-15′,’Tuesday’)
insert into @workingDates values (20191016,’2019-10-16′,’Wednesday’)
insert into @workingDates values (20191017,’2019-10-17′,’Thursday’)
insert into @workingDates values (20191018,’2019-10-18′,’Friday’)
insert into @workingDates values (20191021,’2019-10-21′,’Monday’)
insert into @workingDates values (20191022,’2019-10-22′,’Tuesday’)
insert into @workingDates values (20191023,’2019-10-23′,’Wednesday’)
insert into @workingDates values (20191024,’2019-10-24′,’Thursday’)
insert into @workingDates values (20191025,’2019-10-25′,’Friday’)
insert into @workingDates values (20191028,’2019-10-28′,’Monday’)
insert into @workingDates values (20191029,’2019-10-29′,’Tuesday’)
insert into @workingDates values (20191030,’2019-10-30′,’Wednesday’)
insert into @workingDates values (20191031,’2019-10-31′,’Thursday’)
insert into @workingDates values (20191101,’2019-11-01′,’Friday’)
insert into @workingDates values (20191104,’2019-11-04′,’Monday’)
insert into @workingDates values (20191105,’2019-11-05′,’Tuesday’)
insert into @workingDates values (20191106,’2019-11-06′,’Wednesday’)
insert into @workingDates values (20191107,’2019-11-07′,’Thursday’)
insert into @workingDates values (20191108,’2019-11-08′,’Friday’)
insert into @workingDates values (20191111,’2019-11-11′,’Monday’)
insert into @workingDates values (20191112,’2019-11-12′,’Tuesday’)
insert into @workingDates values (20191113,’2019-11-13′,’Wednesday’)
insert into @workingDates values (20191114,’2019-11-14′,’Thursday’)
insert into @workingDates values (20191115,’2019-11-15′,’Friday’)
insert into @workingDates values (20191118,’2019-11-18′,’Monday’)
insert into @workingDates values (20191119,’2019-11-19′,’Tuesday’)
insert into @workingDates values (20191120,’2019-11-20′,’Wednesday’)
insert into @workingDates values (20191121,’2019-11-21′,’Thursday’)
insert into @workingDates values (20191122,’2019-11-22′,’Friday’)
insert into @workingDates values (20191125,’2019-11-25′,’Monday’)
insert into @workingDates values (20191126,’2019-11-26′,’Tuesday’)
insert into @workingDates values (20191127,’2019-11-27′,’Wednesday’)
insert into @workingDates values (20191128,’2019-11-28′,’Thursday’)
insert into @workingDates values (20191129,’2019-11-29′,’Friday’)
insert into @workingDates values (20191202,’2019-12-02′,’Monday’)
insert into @workingDates values (20191203,’2019-12-03′,’Tuesday’)
insert into @workingDates values (20191204,’2019-12-04′,’Wednesday’)
insert into @workingDates values (20191205,’2019-12-05′,’Thursday’)
insert into @workingDates values (20191206,’2019-12-06′,’Friday’)
insert into @workingDates values (20191209,’2019-12-09′,’Monday’)
insert into @workingDates values (20191210,’2019-12-10′,’Tuesday’)
insert into @workingDates values (20191211,’2019-12-11′,’Wednesday’)
insert into @workingDates values (20191216,’2019-12-16′,’Monday’)
insert into @workingDates values (20191217,’2019-12-17′,’Tuesday’)
insert into @workingDates values (20191218,’2019-12-18′,’Wednesday’)
insert into @workingDates values (20191219,’2019-12-19′,’Thursday’)
insert into @workingDates values (20191220,’2019-12-20′,’Friday’)
insert into @workingDates values (20191224,’2019-12-24′,’Tuesday’)
insert into @workingDates values (20191227,’2019-12-27′,’Friday’)
insert into @workingDates values (20191230,’2019-12-30′,’Monday’)
insert into @workingDates values (20191231,’2019-12-31′,’Tuesday’)
答案 0 :(得分:2)
这是完整的代码
1)我用突尼斯 =>表 @tabholiday
随时用您的国家假日日期填写
2)我生成了具有给定年份(此处为2019)的所有日期的动态表=>表 @calendar
3)天(如果星期五是假日,则为每个月的第二个星期五)存储在表@tab_friday
中4)天(如果星期一为节假日,则在其后的第三个星期五之后的星期一)存储在表@tab_monday中
5)最终结果是这两个表的联合。
通过CTE和Union All使用SQL RECURSIVITY
declare @tabholiday as table(holiday_date date)
declare @tab_friday as table (m int,date_found date)
declare @tab_monday as table (m int,date_found date)
insert into @tabholiday values('2019/01/07'),('2019/01/11'),('2019/03/20'),('2019/04/09'),('2019/05/01'),('2019/07/25'),('2019/08/13'),('2019/10/15')
declare @calendar as table(wd int,fulldate date,dayname varchar(20),m int,weekday_rank_month int,holiday int)
--Script by Kemal AL GAZZAH
--19/04/2019
set dateformat dmy
--Filling the @Calendar Table------------
;WITH cte as (SELECT cast('01/01/2019' as date) as d
UNION ALL SELECT dateadd(day,1,d) AS d FROM cte WHERE year(dateadd(day,1,d))=2019 )
insert into @calendar SELECT cast(replace(d,'-','') as int) wd,d fulldate,
case datepart(w,d)
when 1 then 'Sunday'
when 2 then 'Monday'
when 3 then 'Tuesday'
when 4 then 'Wednesday'
when 5 then 'Thursday'
when 6 then 'Friday'
when 7 then 'Satuday'
end
[dayname] ,month(d) [M]
,row_number() over (partition by datepart(w,d),month(d) order by d) weekday_rank_month,case isnull(t2.holiday_date,'') when '' then 0 else 1 end [holiday]
FROM cte t1 left outer join @tabholiday t2 on t1.d=t2.holiday_date
order by d
OPTION (MAXRECURSION 366);
--1)-Filling Friday Table
with cte as(
select t1.m [Month],t1.fulldate[secondfriday],t1.holiday,t2.fulldate[secondfriday1] from @calendar t1
outer apply(select top 1 * from @calendar t2 where t2.fulldate < t1.fulldate and t2.holiday=0 order by fulldate desc) t2
/*left outer join @calendar t2 on t1.weekday_rank_month=2 and t1.dayname='friday' and t1.holiday=1 and dateadd(day,-1,t1.fulldate)=t2.fulldate and t2.holiday=0
left outer join @calendar t3 on t1.weekday_rank_month=2 and t1.dayname='friday' and t1.holiday=1 and dateadd(day,-2,t1.fulldate)=t3.fulldate and t2.holiday=1*/
where t1.weekday_rank_month=2 and t1.dayname='friday')
insert into @tab_friday select month,case
when holiday =0 then secondfriday
else secondfriday1
end [second friday or before (if that friday is a holiday)] from cte;
--2)-Filling Monday Table
;
with cte as
(
select t1.m[Month],t2.fulldate d1,t2.holiday,t3.fulldate d2 from @calendar t1
outer apply(select top 1 * from @calendar t2 where t2.dayname='monday' and t2.fulldate > t1.fulldate order by fulldate) t2
outer apply (select top 1 * from @calendar t3 where fulldate > t2.fulldate and holiday=0 and month(fulldate)=t1.m order by fulldate ) t3
/*left outer join @calendar t3 on t2.holiday=1 and t3.holiday=0 and dateadd(day,1,t1.fulldate)=t3.fulldate
left outer join @calendar t4 on t2.holiday=1 and t3.holiday=1 and dateadd(day,2,t1.fulldate)=t4.fulldate*/
where t1.weekday_rank_month=3 and t1.dayname='friday')
insert into @tab_monday
select
[Month],
case
when holiday=0 then d1
else d2
end [Monday after third friday(or after f monday is a holiday)] from cte
--select * from @tab_monday
--3)-Jointure Betwwen Friday and Monday Table
select t1.m [Month],t1.date_found [Second Friday or before],t2.date_found[Monday after third friday or after] from @tab_friday t1 inner join @tab_monday t2 on t1.m=t2.m