在t-sql中将时间+持续时间分成间隔

时间:2008-10-31 07:37:16

标签: tsql datetime

有没有人知道解决这个问题的简单方法?

我有一张表,其中包含事件的开始时间和相关的持续时间。我需要能够将事件持续时间分成30分钟的间隔。因此,例如,如果事件在10:45:00开始且持续时间为00:17:00,则返回的集合应分配15分钟到10:30:00间隔和00:02:00分钟到11:00 :00间隔。

我确信我可以找出一种笨拙的方法,但想要更简单一些。我必须经常想到这一点,但Google今天没有任何帮助。

谢谢,

史蒂夫

2 个答案:

答案 0 :(得分:3)

您可以仅使用时间(超过24小时)创建查找表,然后加入该表。您需要将日期重新定义为查找中使用的日期。然后在上下间隔执行一个约会,以计算其持续时间。每个中间间隔为30分钟。

create table #interval_lookup (
  from_date datetime,
  to_date datetime
)

declare @time datetime
set @time = '00:00:00'

while @time < '2 Jan 1900'
  begin
    insert into #interval_lookup values (@time, dateadd(minute, 30, @time))
    set @time = dateadd(minute, 30, @time)
  end

declare @search_from datetime
declare @search_to datetime

set @search_from = '10:45:00'
set @search_to = dateadd(minute, 17, @search_from) 

select
  from_date as interval,
  case
    when from_date <= @search_from and 
         @search_from < to_date and 
         from_date <= @search_to and 
         @search_to < to_date 
         then datediff(minute, @search_from, @search_to)
    when from_date <= @search_from and 
         @search_from < to_date 
         then datediff(minute, @search_from, to_date)
    when from_date <= @search_to and 
         @search_to < to_date then 
         datediff(minute, from_date, @search_to)
    else 30
  end as duration
from
  #interval_lookup
where
  to_date > @search_from
  and from_date <= @search_to

答案 1 :(得分:2)

创建拆分单个事件的TVF:

ALTER FUNCTION dbo.TVF_TimeRange_Split_To_Grid
(
    @eventStartTime datetime
    , @eventDurationMins float
    , @intervalMins int
)
RETURNS @retTable table
(
    intervalStartTime datetime
    ,intervalEndTime datetime
    ,eventDurationInIntervalMins float
)
AS
BEGIN

    declare @eventMinuteOfDay int
    set @eventMinuteOfDay = datepart(hour,@eventStartTime)*60+datepart(minute,@eventStartTime)

    declare @intervalStartMinute int
    set @intervalStartMinute = @eventMinuteOfDay - @eventMinuteOfDay % @intervalMins

    declare @intervalStartTime datetime
    set @intervalStartTime = dateadd(minute,@intervalStartMinute,cast(floor(cast(@eventStartTime as float)) as datetime))

    declare @intervalEndTime datetime
    set @intervalEndTime = dateadd(minute,@intervalMins,@intervalStartTime)

    declare @eventDurationInIntervalMins float

    while (@eventDurationMins>0)
    begin

        set @eventDurationInIntervalMins = cast(@intervalEndTime-@eventStartTime as float)*24*60
        if @eventDurationMins<@eventDurationInIntervalMins 
            set @eventDurationInIntervalMins = @eventDurationMins

        insert into @retTable
        select @intervalStartTime,@intervalEndTime,@eventDurationInIntervalMins

        set @eventDurationMins = @eventDurationMins - @eventDurationInIntervalMins
        set @eventStartTime = @intervalEndTime

        set @intervalStartTime = @intervalEndTime
        set @intervalEndTime = dateadd(minute,@intervalMins,@intervalEndTime)
    end

    RETURN 
END
GO

测试:

select getdate()
select * from dbo.TVF_TimeRange_Split_To_Grid(getdate(),23,30)

测试结果:

2008-10-31 11:28:12.377

intervalStartTime       intervalEndTime         eventDurationInIntervalMins
----------------------- ----------------------- ---------------------------
2008-10-31 11:00:00.000 2008-10-31 11:30:00.000 1,79372222222222
2008-10-31 11:30:00.000 2008-10-31 12:00:00.000 21,2062777777778

样本用法:

select input.eventName, result.* from
(
    select 
        'first' as eventName
        ,cast('2008-10-03 10:45' as datetime) as startTime
        ,17 as durationMins
    union all
    select 
        'second' as eventName
        ,cast('2008-10-05 11:00' as datetime) as startTime
        ,17 as durationMins
    union all
    select 
        'third' as eventName
        ,cast('2008-10-05 12:00' as datetime) as startTime
        ,100 as durationMins
) input
cross apply dbo.TVF_TimeRange_Split_To_Grid(input.startTime,input.durationMins,30) result

样本使用结果:

eventName intervalStartTime       intervalEndTime         eventDurationInIntervalMins
--------- ----------------------- ----------------------- ---------------------------
first     2008-10-03 10:30:00.000 2008-10-03 11:00:00.000 15
first     2008-10-03 11:00:00.000 2008-10-03 11:30:00.000 2
second    2008-10-05 11:00:00.000 2008-10-05 11:30:00.000 17
third     2008-10-05 12:00:00.000 2008-10-05 12:30:00.000 30
third     2008-10-05 12:30:00.000 2008-10-05 13:00:00.000 30
third     2008-10-05 13:00:00.000 2008-10-05 13:30:00.000 30
third     2008-10-05 13:30:00.000 2008-10-05 14:00:00.000 10

(7 row(s) affected)