SQL 两个日期时间之间的工作分钟数

SQL business minutes between two datetimes

我想在 MS SQL server 2008 或更高版本中创建一个函数来计算两个日期时间之间的业务分钟数。我已经测试了在这里和其他网站上找到的 20 多个答案,但还找不到一个始终有效的答案。商家的开始和结束时间为 08:30 和 17:30。请问2016-10-0918:35和2016:-11-099:00的区别。即 9 月 10 日 6:45PM 和 9 月 11 日上午 9 点。我希望回到 30 分钟,但它们都是 return 0。我真的不想一分钟一分钟地循环检查每一分钟,只是一些基本的 SQL "math".

以下SQL可以转换为1个函数或将它们分开。就个人而言,我认为将它们分开更有用。您将拥有更少的接触点,并且可以服务于多个主人。

在业务纪要功能中,我添加了一个 option/illustration 以排除周末 and/or 假期。如果不需要,只需删除。

Date-Range 函数用于生成动态日期范围。它比递归 cte 方法快得多。

Select [dbo].[udf-Business-Minutes]('2016-09-10 18:35','2016-09-11 9:00')

Returns

30

业务分钟函数

ALTER Function [dbo].[udf-Business-Minutes] (@D1 Datetime,@D2 Datetime)
Returns int
Begin
    Return (
            Select Minutes=count(*)
             From [dbo].[udf-Range-Date](case when @D1<=@D2 then @D1 else @D2 end,case when @D1<=@D2 then @D2 else @D1 end,'MI',1)
             Where Cast(RetVal as time) between '08:31' and '17:30'
               and RetVal >case when @D1<=@D2 then @D1 else @D2 end
               and DatePart(DW,RetVal) not in (7,1)  
               and Cast(RetVal as Date) not in (Select Date from (Values 
                                                  ('2016-01-01','New Year''s Day'),
                                                  ('2016-01-18','Martin Luther King, Jr,'),
                                                  ('2016-02-15','Washington''s Birthday'),
                                                  ('2016-03-25','Good Friday'),
                                                  ('2016-05-30','Memorial Day'),
                                                  ('2016-07-04','Independence Day'),
                                                  ('2016-09-05','Labor Day'),
                                                  ('2016-11-24','Thanksgiving'),
                                                  ('2016-11-25','Black Friday'),
                                                  ('2016-12-26','Christmas Day')
                                                  ) as Holidays (Date,Name)
                                              )   
    )
End

通用日期范围函数

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) 
*/

**EDIT - Made a small adjustment to allow for multiple days changed 8:30 to 8:31 to avoid double count **

Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-10 19:00')

Returns 540

同时(跨越两天)

Select [dbo].[udf-Business-Minutes]('2016-09-10 05:00','2016-09-11 19:00')

Returns1080

Select [dbo].[udf-Business-Minutes]('2015-12-31 12:30','2016-01-03 11:30:00')  --Returns 300
Select [dbo].[udf-Business-Minutes]('2016-09-30 05:00','2016-09-30 19:00:00')  --Returns 540
Select [dbo].[udf-Business-Minutes]('2016-09-29 05:00','2016-09-30 19:00:00')  --Returns 1080
Select [dbo].[udf-Business-Minutes]('2015-12-31 12:30','2016-01-03 11:30:00')  --Returns 300