在按时间分组的时间范围内有效的SQL行数

问题描述 投票:1回答:2

SQL Server

我有一个带有2个时间戳的表,time_start和time_end。例如

ID    time_start           time_end
----  -------------------  -------------------
1     2019-01-01 08:30:00  2019-01-01 09:40:00
2     2019-01-01 09:10:24  2019-01-01 15:14:19
3     2019-01-01 09:21:15  2019-01-01 09:21:19
4     2019-01-01 10:39:45  2019-01-01 10:58:12
5     2019-01-01 11:39:45  2019-01-01 11:40:10

并且我想对它们进行分组,这样我可以将行数按可变的时间间隔分组。例如

time_interval         row_count
-------------------   ---------
2019-01-01 07:00:00   0
2019-01-01 08:00:00   1
2019-01-01 09:00:00   3
2019-01-01 10:00:00   2
2019-01-01 11:00:00   1
2019-01-01 12:00:00   0

我的时间间隔可能是1小时,1分钟,30分钟,1天等...

将其视为登录/注销的情况,我想看看在给定的分钟,小时,天等情况下用户如何登录?

sql sql-server timestamp grouping intervals
2个回答
0
投票

嗨,这是我的解决方法。我用您的数据创建了一个表“ test”。

首先,我获得最小和最大间隔,然后,我使用CTE获得这些值之间的所有间隔。最后,有了这个CTE和一个在time_start和time_end之间间隔的左连接,我得到了答案。

此间隔为1小时

DECLARE @minDate AS DATETIME;
DECLARE @maxDate AS DATETIME;

SET @minDate = (select case 
when (select min(time_start) from test) < (select min(time_end) from test) 
then (select min(time_start) from test)
else (select min(time_end) from test) end )
SET @minDate = FORMAT(@minDate, 'dd-MM.yyyy HH:00:00')

SET @maxDate = (select case 
when (select max(time_start) from test) > (select max(time_end) from test) 
then (select max(time_start) from test)
else (select max(time_end) from test) end )
SET @maxDate = FORMAT(@maxDate, 'dd-MM.yyyy HH:00:00')

;WITH Dates_CTE
     AS (SELECT @minDate AS Dates
         UNION ALL
         SELECT Dateadd(hh, 1, Dates)
         FROM   Dates_CTE
         WHERE  Dates < @maxDate)

SELECT d.Dates as time_interval, count(*) as row_count
FROM   Dates_CTE d
LEFT JOIN test t on d.Dates 
between (FORMAT(t.time_start, 'dd-MM.yyyy HH:00:00')) 
and (FORMAT(t.time_end, 'dd-MM.yyyy HH:00:00'))  
GROUP BY d.Dates

每隔10分钟,您需要进行一些更改。首先,我格式化日期以获取分钟数(dd-MM.yyyy HH:mm:00 instead of dd-MM.yyyy HH:00:00)然后在左侧联接中,将time_start和time_end接近其10分钟的时间(9:30:00中的9:32:00)(dateadd(minute, 10 * (datediff(minute, 0, time_start) / 10), 0))

DECLARE @minDate AS DATETIME;
DECLARE @maxDate AS DATETIME;

SET @minDate = (select case 
when (select min(time_start) from test) < (select min(time_end) from test) 
then (select min(time_start) from test)
else (select min(time_end) from test) end )
SET @minDate = FORMAT(@minDate, 'dd-MM.yyyy HH:mm:00')

SET @maxDate = (select case 
when (select max(time_start) from test) > (select max(time_end) from test) 
then (select max(time_start) from test)
else (select max(time_end) from test) end )
SET @maxDate = FORMAT(@maxDate, 'dd-MM.yyyy HH:mm:00')

;WITH Dates_CTE
     AS (SELECT @minDate AS Dates
         UNION ALL
         SELECT Dateadd(minute, 10, Dates)
         FROM   Dates_CTE
         WHERE  Dates < @maxDate)

SELECT d.Dates as time_interval, count(*) as row_count
FROM   Dates_CTE d
LEFT JOIN test t on d.Dates 
between dateadd(minute, 10 * (datediff(minute, 0, time_start) / 10), 0) 
and dateadd(minute, 10 * (datediff(minute, 0, time_end) / 10), 0)
GROUP BY d.Dates

最后我以1小时的间隔得到了这个结果:

+---------------------+-----------+
|    time_interval    | row_count |
+---------------------+-----------+
| 01/01/2019 08:00:00 |         1 |
| 01/01/2019 09:00:00 |         3 |
| 01/01/2019 10:00:00 |         2 |
| 01/01/2019 11:00:00 |         2 |
| 01/01/2019 12:00:00 |         1 |
| 01/01/2019 13:00:00 |         1 |
| 01/01/2019 14:00:00 |         1 |
| 01/01/2019 15:00:00 |         1 |
+---------------------+-----------+

我希望它对您有用。


0
投票

尝试一下,

    DECLARE @start_date datetime='2019-01-01',
            @end_date datetime='2019-01-02',
            @i_minutes int=60

    DECLARE @t TABLE
    (
        id int identity(1,1),time_start datetime,time_end datetime
    )
    INSERT INTO @t(time_start,time_end)VALUES
    ('2019-01-01 08:30:00','2019-01-01 09:40:00'),
    ('2019-01-01 09:10:24','2019-01-01 15:14:19'),
    ('2019-01-01 09:21:15','2019-01-01 09:21:19'),
    ('2019-01-01 10:39:45','2019-01-01 10:58:12'),
    ('2019-01-01 11:39:45','2019-01-01 11:40:10')

    --SELECT @start_date=min(time_start),@end_date=max(time_end)
    --FROM @t

    ;WITH CTE_time_Interval AS
    (
        SELECT @start_date AS time_int,@i_minutes AS i_minutes
        UNION ALL
        SELECT dateadd(minute,@i_minutes,time_int),i_minutes+ @i_minutes
        FROM CTE_time_Interval
        WHERE time_int<=@end_date
    )
    ,CTE1 AS
    (
        SELECT ROW_NUMBER()OVER(ORDER BY time_int)AS r_no,time_int 
        FROM CTE_time_Interval
    )
    ,CTE2 AS
    (
        SELECT a.time_int AS Int_start_time,b.time_int AS Int_end_time 
        FROM CTE1 a
        INNER JOIN CTE1 b ON a.r_no+1=b.r_no
    )

    SELECT a.Int_start_time,a.Int_end_time,sum(iif(b.time_start is not null,1,0)) AS cnt
FROM CTE2 a
LEFT JOIN @t b ON
(
    b.time_start BETWEEN a.Int_start_time AND a.Int_end_time 
    OR 
    b.time_end BETWEEN a.Int_start_time AND a.Int_end_time
    OR
    a.Int_start_time BETWEEN b.time_start AND b.time_end
    OR
    a.Int_end_time BETWEEN b.time_start AND b.time_end 
)
GROUP BY a.Int_start_time,a.Int_end_time
© www.soinside.com 2019 - 2024. All rights reserved.