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

How to get occurence every n weeks in SQL/TSQL

我在 DLL 中有一个名为 CronTabSchedule 的函数,用于从 CRON 表达式生成日期出现。 我添加了一个列,其中 return 是每个日期的周数

函数调用如下:

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

所以我的 CRON 表达式是 'every tuesday at 18:00'

它 return 一个 table 像那样 :

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

我的问题是 CRON 表达式无法处理 'every n weeks'

所以我想在 SQL 或 T-SQL 中,每 n 周 table 获取所有出现的事件

例如:每 2 周将 return :

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

例 2:每 3 周 return :

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

我经常使用 Table-Valued-Function 来创建动态 date/time 范围。比递归 cte 更快(尤其是对于更大的范围)并提供更多功能:用户提供的 Date/Time 范围、日期部分和增量。

也很容易合并到交叉应用中

示例 1

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

Returns

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)

Returns

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

感兴趣的 UDF

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<=@R2
)
/*
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) 
*/

我认为您的函数有误,因为周数不正确。但如果是,这将起作用:

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.

这是一个简单的解决方案:

(每 2 周一次)

Select * From Table
where week_number %2 = 1

(每 3 周一次)

Select * From Table
where week_number %3 = 1