SQL Server,如何对接近时间的行进行分组

时间:2018-08-20 14:47:44

标签: sql-server

我有一个具有时间值和用户ID的表,并且我想对行进行分组(如果它们在时间上接近)(每行之间少于2百万),并按用户ID进行分组。

这里是一个示例:

CreatedAt  | User ID
'16:01:01' | '01'
'16:02:20' | '01'
'16:03:20' | '01'
'16:04:20' | '01'
'16:05:20' | '02'
'16:06:20' | '02'
'16:07:20' | '02'
'16:08:20' | '02'
'16:14:02' | '02'
'16:15:01' | '02'
'16:20:02' | '03'

结果应为:
用户ID = 01

'16:01:01'
'16:02:20'
'16:03:20'
'16:04:20'

用户ID = 02

'16:05:20'
'16:06:20'
'16:07:20'
'16:08:20'

'16:14:02'
'16:15:01'

用户ID = 03

'16:20:02'

我什至不确定它是否可以通过SQL进行操作,或者我必须对其进行编码(我的数据库中有几百万行,所以这不是最有效的方法)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这将为组分配“组号”。但是,不确定该实现了什么,但可能会帮助您在表示层上实现所需的目标:

WITH VTE AS(
    SELECT CONVERT(time(0), V.CreatedAt) AS CreatedAt, UserID
    FROM (VALUES ('16:01:01','01'),
                 ('16:02:20','01'),
                 ('16:03:20','01'),
                 ('16:04:20','01'),
                 ('16:05:20','02'),
                 ('16:06:20','02'),
                 ('16:07:20','02'),
                 ('16:08:20','02'),
                 ('16:14:02','02'),
                 ('16:15:01','02'),
                 ('16:20:02','03')) V(CreatedAt, UserID)),
TimeDiff AS(
    SELECT *,
           CASE WHEN DATEDIFF(SECOND,LAG(CreatedAt,1,CreatedAt) OVER (PARTITION BY UserID ORDER BY CreatedAt ASC),CreatedAt) <= 120 THEN 1 ELSE 0 END AS Succession
    FROM VTE)
SELECT TD.CreatedAt,
       TD.UserID,
       COUNT(CASE WHEN TD.Succession = 0 THEN 1 END) OVER (PARTITION BY UserID ORDER BY TD.CreatedAt
                                                           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS GroupNumber
FROM TimeDiff TD;