SQL 服务器中基于 10 秒时间间隔的记录分组
Group By records based on 10 second time interval in SQL server
要求按10秒时间间隔对table条记录进行分组。鉴于 table
Id DateTime Rank
1 2011-09-27 18:36:15 1
2 2011-09-27 18:36:15 1
3 2011-09-27 18:36:19 1
4 2011-09-27 18:36:23 1
5 2011-09-27 18:36:26 1
6 2011-09-27 18:36:30 1
7 2011-09-27 18:36:32 1
8 2011-09-27 18:36:14 2
9 2011-09-27 18:36:16 2
10 2011-09-27 18:36:35 2
分组应该是这样的
Id DateTime Rank GroupRank
1 2011-09-27 18:36:15 1 1
2 2011-09-27 18:36:15 1 1
3 2011-09-27 18:36:19 1 1
4 2011-09-27 18:36:23 1 1
5 2011-09-27 18:36:26 1 2
6 2011-09-27 18:36:30 1 2
7 2011-09-27 18:36:32 1 2
8 2011-09-27 18:36:14 2 3
9 2011-09-27 18:36:16 2 3
10 2011-09-27 18:36:35 2 4
对于等级 1 最短时间是 18:36:15 并且基于 18:36:15 到 18:36:24 之间的所有记录应该在一个组中等等。
我希望 GroupRank 处于相同 table。所以这将是 dense_Rank() Over 子句。谁能帮我在 SQL.
中编写查询
您需要分两步完成此操作,首先是将每条记录分成 10 秒组,方法是获取与每个等级的最短时间相差的秒数,除以 10,然后四舍五入向下到最接近的整数。
SELECT *,
SecondGroup = FLOOR(DATEDIFF(SECOND,
MIN([DateTime]) OVER(PARTITION BY [Rank]),
[DateTime]) / 10.0)
FROM #T;
给出:
Id DateTime Rank SecondGroup
---------------------------------------------------
1 2011-09-27 18:36:15.000 1 0
2 2011-09-27 18:36:15.000 1 0
3 2011-09-27 18:36:19.000 1 0
4 2011-09-27 18:36:23.000 1 0
5 2011-09-27 18:36:26.000 1 1
6 2011-09-27 18:36:30.000 1 1
7 2011-09-27 18:36:32.000 1 1
8 2011-09-27 18:36:14.000 2 0
9 2011-09-27 18:36:16.000 2 0
10 2011-09-27 18:36:35.000 2 2
然后您可以通过 Rank
和 SecondGroup
:
进行 DENSE_RANK
排序
SELECT Id, [DateTime], [Rank],
GroupRank = DENSE_RANK() OVER(ORDER BY [Rank], SecondGroup)
FROM ( SELECT *,
SecondGroup = FLOOR(DATEDIFF(SECOND,
MIN([DateTime]) OVER(PARTITION BY [Rank]),
[DateTime]) / 10.0)
FROM #T
) AS t;
这会给出您想要的输出。
示例数据
CREATE TABLE #T (Id INT, [DateTime] DATETIME, [Rank] INT);
INSERT #T (Id, [DateTime], [Rank])
VALUES
(1, '2011-09-27 18:36:15', 1),
(2, '2011-09-27 18:36:15', 1),
(3, '2011-09-27 18:36:19', 1),
(4, '2011-09-27 18:36:23', 1),
(5, '2011-09-27 18:36:26', 1),
(6, '2011-09-27 18:36:30', 1),
(7, '2011-09-27 18:36:32', 1),
(8, '2011-09-27 18:36:14', 2),
(9, '2011-09-27 18:36:16', 2),
(10, '2011-09-27 18:36:35', 2);
要求按10秒时间间隔对table条记录进行分组。鉴于 table
Id DateTime Rank
1 2011-09-27 18:36:15 1
2 2011-09-27 18:36:15 1
3 2011-09-27 18:36:19 1
4 2011-09-27 18:36:23 1
5 2011-09-27 18:36:26 1
6 2011-09-27 18:36:30 1
7 2011-09-27 18:36:32 1
8 2011-09-27 18:36:14 2
9 2011-09-27 18:36:16 2
10 2011-09-27 18:36:35 2
分组应该是这样的
Id DateTime Rank GroupRank
1 2011-09-27 18:36:15 1 1
2 2011-09-27 18:36:15 1 1
3 2011-09-27 18:36:19 1 1
4 2011-09-27 18:36:23 1 1
5 2011-09-27 18:36:26 1 2
6 2011-09-27 18:36:30 1 2
7 2011-09-27 18:36:32 1 2
8 2011-09-27 18:36:14 2 3
9 2011-09-27 18:36:16 2 3
10 2011-09-27 18:36:35 2 4
对于等级 1 最短时间是 18:36:15 并且基于 18:36:15 到 18:36:24 之间的所有记录应该在一个组中等等。
我希望 GroupRank 处于相同 table。所以这将是 dense_Rank() Over 子句。谁能帮我在 SQL.
中编写查询您需要分两步完成此操作,首先是将每条记录分成 10 秒组,方法是获取与每个等级的最短时间相差的秒数,除以 10,然后四舍五入向下到最接近的整数。
SELECT *,
SecondGroup = FLOOR(DATEDIFF(SECOND,
MIN([DateTime]) OVER(PARTITION BY [Rank]),
[DateTime]) / 10.0)
FROM #T;
给出:
Id DateTime Rank SecondGroup
---------------------------------------------------
1 2011-09-27 18:36:15.000 1 0
2 2011-09-27 18:36:15.000 1 0
3 2011-09-27 18:36:19.000 1 0
4 2011-09-27 18:36:23.000 1 0
5 2011-09-27 18:36:26.000 1 1
6 2011-09-27 18:36:30.000 1 1
7 2011-09-27 18:36:32.000 1 1
8 2011-09-27 18:36:14.000 2 0
9 2011-09-27 18:36:16.000 2 0
10 2011-09-27 18:36:35.000 2 2
然后您可以通过 Rank
和 SecondGroup
:
DENSE_RANK
排序
SELECT Id, [DateTime], [Rank],
GroupRank = DENSE_RANK() OVER(ORDER BY [Rank], SecondGroup)
FROM ( SELECT *,
SecondGroup = FLOOR(DATEDIFF(SECOND,
MIN([DateTime]) OVER(PARTITION BY [Rank]),
[DateTime]) / 10.0)
FROM #T
) AS t;
这会给出您想要的输出。
示例数据
CREATE TABLE #T (Id INT, [DateTime] DATETIME, [Rank] INT);
INSERT #T (Id, [DateTime], [Rank])
VALUES
(1, '2011-09-27 18:36:15', 1),
(2, '2011-09-27 18:36:15', 1),
(3, '2011-09-27 18:36:19', 1),
(4, '2011-09-27 18:36:23', 1),
(5, '2011-09-27 18:36:26', 1),
(6, '2011-09-27 18:36:30', 1),
(7, '2011-09-27 18:36:32', 1),
(8, '2011-09-27 18:36:14', 2),
(9, '2011-09-27 18:36:16', 2),
(10, '2011-09-27 18:36:35', 2);