2017-03-07 16 views
1

我在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

回答

0

我覺得當周以來的數字是不正確的函數有錯誤的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

哦,是的,我再次嘗試,它的作品我的不好^^ –

1

我經常使用表值函數來創建動態日期/時間範圍。比遞歸更快(特別是對於更大的範圍)並提供更多功能:用戶提供的日期/時間範圍,日期部分和增量。

而且容易合併到一個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) 
*/ 
-1

這裏有一個簡單的解決方案:

(每2周)

Select * From Table 
where week_number %2 = 1 

(每3周)

Select * From Table 
where week_number %3 = 1 
相關問題