2

我想编写一个查询,该查询将仅使用与活动开始和停止时间相对应的时间戳来计算一天中每 15 分钟间隔内发生的活动总量。

这是一个示例数据集:

    DATE    StartDateTime   StopDateTime
2/2/2015    2/2/2015 7:00   2/2/2015 7:25
2/2/2015    2/2/2015 7:20   2/2/2015 7:29
2/2/2015    2/2/2015 7:35   2/2/2015 7:42
2/2/2015    2/2/2015 8:05   2/2/2015 8:14
2/2/2015    2/2/2015 8:16   2/2/2015 8:20
2/2/2015    2/2/2015 8:29   2/2/2015 8:40
2/2/2015    2/2/2015 8:55   2/2/2015 9:25

这就是我希望能够得到的:

    DATE    Interval       activityTime(min)
2/2/2015    2/2/2015 7:00   15
2/2/2015    2/2/2015 7:15   19
2/2/2015    2/2/2015 7:30   7
2/2/2015    2/2/2015 7:45   0
2/2/2015    2/2/2015 8:00   9
2/2/2015    2/2/2015 8:15   5
2/2/2015    2/2/2015 8:30   10
2/2/2015    2/2/2015 8:45   5
2/2/2015    2/2/2015 9:00   15
2/2/2015    2/2/2015 9:15   10

我一直在寻找一种以我需要的方式组织数据的方法,这是迄今为止我能找到的最接近的方法,尽管我无法让它工作:

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

我对 SQL 很陌生,所以对解决方案的任何解释都将不胜感激。这也是我在 stackoverflow 上的第一篇文章,所以如果数据不是首选格式或有任何其他问题,请告诉我。谢谢!

4

2 回答 2

2

Assuming some reasonable recent version of SQL Server, this ought to be a good start:

-- Some sample data.
declare @Samples as Table ( SampleId Int Identity, Start DateTime, Stop DateTime );
insert into @Samples ( Start, Stop ) values
  ( '2/2/2015 7:00', '2/2/2015 7:25' ),
  ( '2/2/2015 7:20', '2/2/2015 7:29' ),
  ( '2/2/2015 7:35', '2/2/2015 7:42' ),
  ( '2/2/2015 8:05', '2/2/2015 8:14' ),
  ( '2/2/2015 8:16', '2/2/2015 8:20' ),
  ( '2/2/2015 8:29', '2/2/2015 8:40' ),
  ( '2/2/2015 8:55', '2/2/2015 9:25' );
select * from @Samples;

-- Find the limits and align them to quarter hours.
declare @Min as DateTime;
declare @Max as DateTime;
select @Min = min( Start ), @Max = max( Stop )
  from @Samples;
set @Min = DateAdd( minute, -DatePart( minute, @Min ) % 15, @Min );
set @Max = DateAdd( minute, 15 - DatePart( minute, @Max ) % 15, @Max );
select @Min as [Min], @Max as [Max];


-- Go for it.
with QuarterHours ( QuarterStart, QuarterStop )
  as (
    select @Min, DateAdd( minute, 15, @Min )
    union all
    select QuarterStop, DateAdd( minute, 15, QuarterStop )
      from QuarterHours
      where QuarterStop < @Max ),
  Overlaps
  as ( select QH.QuarterStart, QH.QuarterStop, S.Start, S.Stop,
  case
    when S.Start <= QH.QuarterStart and S.Stop >= QH.QuarterStop then 15
    when S.Start <= QH.QuarterStart and S.Stop < QH.QuarterStop then DateDiff( minute, QH.QuarterStart, S.Stop )
    when S.Start > QH.QuarterStart and S.Stop >= QH.QuarterStop then DateDiff( minute, S.Start, QH.QuarterStop )
    when S.Start > QH.QuarterStart and S.Stop < QH.QuarterStop then DateDiff( minute, S.Start, S.Stop )
    else 0 end as Overlap
  from QuarterHours as QH left outer join
    @Samples as S on S.Start <= QH.QuarterStop and S.Stop >= QH.QuarterStart )
  select QuarterStart, sum( Overlap ) as [ActivityTime]
    from Overlaps
    group by QuarterStart
    order by QuarterStart;

You can change the last select to either select * from QuarterHours or select * from Overlaps to see some of the intermediate values.

Explanatory notes:

You can use any range (@Min/@Max) you want, I just took them from the sample data so that the example would run. I used a table variable for the same reason, no need to create a "real" table for the sake of an example.

The Common Table Expression (CTE) creates, via recursion, a table of QuarterHours that covers the desired range. (A numbers table or tally table could also be used to generate the quarter hours.) Then a LEFT OUTER JOIN with the sample data is used to locate all of the Overlaps, if any, with each quarter hour. That preserves the quarter hours for which there is no activity.

The final SELECT summarizes the results.

于 2015-04-09T00:13:34.210 回答
1

The following query will give you each 15-minute increment that contains at least one start time and the total amount (in minutes) of activity for the entire duration that started in that 15-minute increment.

select  Date,
        Convert( SmallDatetime, Floor( Cast( StartDateTime as float ) * 96.0 ) / 96.0 ) Increment,
        Sum( DateDiff( second, StartDateTime, StopDateTime )) / 60 Duration
from    Activities
group by Date, Convert( SmallDatetime, Floor( Cast( StartDateTime as float ) * 96.0 ) / 96.0 );

Which returns this:

Date       Increment           Duration
---------- ------------------- --------
2015-02-02 2015-02-02 07:00:00 25
2015-02-02 2015-02-02 07:15:00  9
2015-02-02 2015-02-02 07:30:00  7
2015-02-02 2015-02-02 08:00:00  9
2015-02-02 2015-02-02 08:15:00 15
2015-02-02 2015-02-02 08:45:00 30

I was just looking into calculating the running total with overflow into the next increment, when a coupla things occurred to me. One is that you're going to need every 15-minute increment during the time of your query, whether any activity starts in it or not. So we would have to use a tally table to ensure every interval is generated, if nothing else, to catch some overflow minutes from the previous interval.

Then, of course, there is tracking the running total with overflow. While this is possible (see https://stackoverflow.com/a/861073/3658753 for a good explanation of how), it hit me that the combination of the two (tally table and running total) is an awful lot of overhead to be performed in SQL. Remember that performing calculations in SQL is many times faster than even the fastest disk access, but performing calculations in any high level language (Java, C++, C# or even scripting languages like Perl) is going to be many times faster than SQL. Plus the maintainability of the SQL solution will be deep in the basement.

So my recommendation at this point is to take the query above and feed it into a good reporting engine or your application and have them perform the additional calculations. Performance-wise, you'll be way ahead.

于 2015-04-09T04:55:47.530 回答