您可以使用下面的函数以分钟为单位计算活动,我假设一周的第一天是星期一,否则您应该修改与计算星期几(DatePart)相关的行
create function fn_activity_minutes(@Datetime1 DateTime,@Datetime2 DateTime)
Returns BigInt
as
Begin
Declare
@Date1 Date,
@Date2 Date,
@DateIndex Date,
@minutes int,
@lastDayMinutes int
Set @Date1 = Convert(Date,@DateTime1)
Set @Date2 = Convert(Date,@DateTime2)
Set @minutes=DateDiff(minute,@DateTime1,DateAdd(Hour,22,convert(DateTime,@Date1)))
if @minutes<0
Set @minutes=0
Set @DateIndex=DateAdd(day,1,@Date1)
While @DateIndex<@Date2
Begin
if DatePart(dw,@DateIndex) not in (6,7) -- you can even check holdays here
set @minutes=@minutes+16*60 -- 16 hours activity per day
Set @DateIndex=DateAdd(day,1,@DateIndex)
End
if DatePart(dw,@DateIndex) not in (6,7) -- you can even check holdays here
Begin
set @lastDayMinutes=DateDiff(minute,DateAdd(Hour,6,convert(DateTime,@Date2)),@DateTime2)
if @lastDayMinutes>16*60
set @lastDayMinutes=16*60
if @Date1<>@Date2
set @minutes=@minutes+@lastDayMinutes
Else
Set @minutes=@minutes+@lastDayMinutes-16*60
End
return @minutes
End