简体   繁体   English

在SQL Server中创建时间间隔

[英]Creating time intervals in SQL Server

I have a table in SQL Server that stores events and the datetime of the event. 我在SQL Server中有一个表,用于存储事件和事件的datetime时间。 I am now looking to have these events grouped into eg 10 minute intervals showing how many events in each group. 我现在希望将这些事件分组为例如10分钟的间隔,以显示每组中有多少事件。

I am looking for a result similar to the following. 我正在寻找与以下类似的结果。 Does anyone have an example og how this could be done? 有没有人举个例子说明如何做到这一点?

    FromTIme                | ToTime                  | Number of Events
    -----------------------------------------------------------------------
    2012-12-03 00:00:00.000 | 2012-12-03 00:10:00.000 |     5
    2012-12-03 00:10:00.000 | 2012-12-03 00:20:00.000 |    15

The query will be used in a report so I would like to be able to set the @StartDate and @EndDate of the interval. 该查询将在报表中使用,因此我希望能够设置间隔的@StartDate@EndDate

You can do this: 你可以这样做:

DECLARE @startDate DATETIME = '2012-12-03 00:00:00.000';
DECLARE @endDate   DATETIME = '2012-12-03 00:20:00.000';
DECLARE @counter INT = 1;
DECLARE @nOfIntervals INT = DATEDIFF(minute, @startDate, @endDate) / 10;

;WITH Temp
AS
(
    SELECT n
    FROM (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) Nums(n)
), Numbers
AS
(
    SELECT n = t2.n * 10 + t1.n + 0
    FROM Temp t1
    CROSS JOIN Temp t2
), FromToCTE
AS
(
    SELECT  
      DATEADD(minute, 10 * n, @startDate) FromDate, 
      DATEADD(minute, 10 *(n +1), @startDate) ToDate
    FROM numbers 
    WHERE (n + 1) <= @nOfIntervals
 ),EventsFromTo
 AS
 (
     SELECT 
       c.FromDate,
       c.ToDate,
       e.EventDate
     FROM @Events e
     INNER JOIN FromToCTE c ON e.EventDate BETWEEN c.FromDate AND c.ToDate
 )  
 SELECT FromDate, ToDate, COUNT(*) "Number of Events"
 FROM EventsFromTo
 GROUP BY FromDate, ToDate;

Live Demo 现场演示

This will give you something like: 这将为您提供以下信息:

     FromDate                 ToDate          Number of Events
2012-12-03 00:00:00     2012-12-03 00:10:00          5
2012-12-03 00:10:00     2012-12-03 00:20:00          15 

How does this work? 这是如何运作的?

I used a temp table with the numbers from 0 to 9 as an anchor table: 我使用了一个临时表,其中的数字从0到9作为锚表:

SELECT n
FROM (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) Nums(n)

To generate number from 0 to 99: 生成0到99之间的数字:

Numbers
AS
(
    SELECT n = t2.n * 10 + t1.n + 0
    FROM Temp t1
    CROSS JOIN Temp t2
 )

I used this table Numbers for generating the time intervals from the @startDate to @endDate , I assumed that the difference between the two dates won't exceed 99 minutes intervals. 我使用此表Numbers来生成从@startDate@endDate的时间间隔,我假设两个日期之间的时差不会超过99分钟。 If you want to make your query work for more ranges greater than 99, just cross join the table temp more times to generate more numbers. 如果要使查询在大于99的更大范围内工作,只需将表temp交叉连接更多次以生成更多数字。 Later I used these generated numbers to generate the datetimes intervals with 10 minutes interval between each of them. 后来,我使用这些生成的数字来生成日期时间间隔,它们之间的间隔为10分钟。 After that I joined this generated table with the Events table on the event date field. 之后,我加入了这个生成的表与Events有关的事件日期字段的表。 With a GROUP BY and COUNT we are done. 有了GROUP BYCOUNT我们就完成了。

Note that: I didn't check whether the date difference passed to the query using the two parameters @startDate and @EndDate are dividable by 10 minutes or not, I think you might need to check this in your case, and I will leave it as a homework for you. 请注意:我没有检查使用两个参数@startDate@EndDate传递给查询的日期差是否可以被10分钟@EndDate ,我认为您可能需要检查此情况,我将保留它作为您的家庭作业。

This might be a lot of work, but I didn't find an easy way for doing this without using a cursor 这可能需要很多工作,但是我没有找到一种不使用游标的简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM