如何在SQL/TSQL每n周发生一次

问题描述:

我在DLL中有一个函数名CronTabSchedule,用于从CRON表达式生成日期。 我添加了返回每个日期如何在SQL/TSQL每n周发生一次

这里的周数是函数的调用列:

SELECT DATEPART(wk, Occurrence) as week_number, Occurrence FROM dbo.CrontabSchedule('0 18 * * 2', '2017-3-7', '2017-3-31') 

所以我的cron表达式是“每周二18:00”

它返回一个表像:

Occurence week_number 
2017/03/07  8 
2017/03/14  9 
2017/03/21  10 
2017/03/28  11 

我的问题是CRON表达式无法处理“每N周

所以我想,在SQL或T-SQL,拿到表的所有occurence(S)每N周

例如:每2周将返回:

Occurence week_number 
2017/03/07  8 
2017/03/21  10 

练习2:每3周将返回:

Occurence week_number 
2017/03/07  8 
2017/03/28  11 
+1

'DATEPART (wk,'2017年3月7日')是10而不是8,但是,没有?' – scsimon

+1

[踢坏的习惯:使用日期/时间操作的速记 - Aaron Bertrand - 2011-0 9-20](HTTP:// sqlblog。com/blogs/aaron_bertrand/archive/2011/09/20/bad-habit-to-kick-using-shorthand-with-date-time-operations.aspx) – SqlZim

+0

好点@SqlZim ...我alwyas必须参考速记查看正在带回的内容。 – scsimon

我觉得当周以来的数字是不正确的函数有错误的UDF。但是,如果他们成功了,这会工作:

declare @table table (dates datetime, week_number int) 
insert into @table values 
('2017/03/07',10), 
('2017/03/14',11), 
('2017/03/21',12), 
('2017/03/28',13) 



declare @NumberOfWeeks int = 3 


select 
    * 
from @table 
where 
    week_number%@NumberOfWeeks = datepart(week,'2017-3-7')%@NumberOfWeeks 
--just be sure to set the start date of your cron job in this where clause 
--or you could make it a parameter. 
+0

每三周不适用 –

+0

为什么不呢?为我工作 – scsimon

+0

哦,是的,我再次尝试,它的作品我的不好^^ –

我经常使用表值函数来创建动态日期/时间范围。比递归更快(特别是对于更大的范围)并提供更多功能:用户提供的日期/时间范围,日期部分和增量。

而且容易合并到一个CROSS APPLY

实施例1

Select * from [dbo].[udf-Range-Date]('2017-03-07','2017-04-01','WK',1) 

返回

RetSeq RetVal 
1  2017-03-07 00:00:00.000 
2  2017-03-14 00:00:00.000 
3  2017-03-21 00:00:00.000 
4  2017-03-28 00:00:00.000 

实施例2

Select * from [dbo].[udf-Range-Date]('2017-03-07','2017-04-01','WK',2) 

返回

RetSeq RetVal 
1  2017-03-07 00:00:00.000 
2  2017-03-21 00:00:00.000 

如果有兴趣

CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int) 
Returns Table 
Return (
    with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End), 
     cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)), 
     cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h), 
     cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2) 

    Select RetSeq = N+1 
      ,RetVal = D 
    From cte3,cte0 
    Where D<[email protected] 
) 
/* 
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS 
Syntax: 
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1) 
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1) 
*/ 

这里有一个简单的解决方案:

(每2周)

Select * From Table 
where week_number %2 = 1 

(每3周)

Select * From Table 
where week_number %3 = 1