Group by Time Interval

前端 未结 2 1084
醉梦人生
醉梦人生 2021-01-28 02:46

I need to Group my Table into 15 minutes Intervals. I can do that with:

select dateadd(minute, datediff(minute, 0, ts) / 15 * 15, 0), sum (goodpieces) 
from Stat         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-28 03:30

    Something like this might help you.

    declare @startDate datetime
            ,@endDate datetime
    
    set @startDate = '2011-10-25'
    set @endDate = '2011-10-26'
    
    ;with fifteenMinutes
    as
    (
        select  dateadd(minute, datediff(minute, 0, @startDate) / 15 * 15, 0) as q
        UNION ALL
        select  dateadd(minute, 15, q)
        from fifteenMinutes
        where q < @endDate
    )
    
    select * from fifteenMinutes
    

提交回复
热议问题