排名:如何将 ROW_NUMBER 或 RANK 重置为 1
Ranking: How to reset the ROW_NUMBER or RANK to 1
使用 SQL 服务器 2014:
考虑以下 table:
DECLARE @Table TABLE (
Id int NOT NULL identity(1,1),
Col_Value varchar(2)
)
INSERT INTO @Table (Col_Value)
VALUES ('A'),('A'),('B'),('B'),('B'),('A'),('A'),('B'),('B'),('B'),('A'),('B'),('B'),('A'),('A'),('B'),('C'),('C'),('A'),('A'),('B'),('B'),('C')
如何创建一个查询,在结果中生成 R 列,如下所示
+----+------+---+
| ID | Data | R |
+----+------+---+
| 1 | A | 1 |
+----+------+---+
| 2 | A | 2 |
+----+------+---+
| 3 | B | 1 |
+----+------+---+
| 4 | B | 2 |
+----+------+---+
| 5 | B | 3 |
+----+------+---+
| 6 | A | 1 |
+----+------+---+
| 7 | A | 2 |
+----+------+---+
| 8 | B | 1 |
+----+------+---+
| 9 | B | 2 |
+----+------+---+
| 10 | B | 3 |
+----+------+---+
| 11 | A | 1 |
+----+------+---+
| 12 | B | 1 |
+----+------+---+
| 13 | B | 2 |
+----+------+---+
| 14 | A | 1 |
+----+------+---+
| 15 | A | 2 |
+----+------+---+
| 16 | B | 1 |
+----+------+---+
| 17 | C | 1 |
+----+------+---+
| 18 | C | 2 |
+----+------+---+
| 19 | A | 1 |
+----+------+---+
| 20 | A | 2 |
+----+------+---+
| 21 | B | 1 |
+----+------+---+
| 22 | B | 2 |
+----+------+---+
| 23 | C | 1 |
+----+------+---+
在上面的结果table中,一旦数据列在一行中发生变化,R值就会重置为1
更新 1
Ben Thul 的回答非常有效。
我建议在下面 post 更新参考这个答案。
T-sql Reset Row number on Field Change
这在文献中称为 "gaps and islands" 问题。首先,我提出的解决方案:
with cte as (
select *, [Id] - row_number() over (partition by [Col_Value] order by [Id]) as [GroupID]
from @table
)
select [Id], [Col_Value], row_number() over (partition by [GroupID], [Col_Value] order by [Id])
from cte
order by [Id];
为了说明,请注意,如果我使用 row_number()
枚举所有 "A" 值,那些连续的 row_number()
值将以与 ID 相同的速率上升价值。也就是说,他们的差异对于那个连续组(也称为 "island")中的那些人来说是相同的。一旦我们计算出该组标识符,只需枚举每个组的每个成员即可。
使用 SQL 服务器 2014:
考虑以下 table:
DECLARE @Table TABLE (
Id int NOT NULL identity(1,1),
Col_Value varchar(2)
)
INSERT INTO @Table (Col_Value)
VALUES ('A'),('A'),('B'),('B'),('B'),('A'),('A'),('B'),('B'),('B'),('A'),('B'),('B'),('A'),('A'),('B'),('C'),('C'),('A'),('A'),('B'),('B'),('C')
如何创建一个查询,在结果中生成 R 列,如下所示
+----+------+---+
| ID | Data | R |
+----+------+---+
| 1 | A | 1 |
+----+------+---+
| 2 | A | 2 |
+----+------+---+
| 3 | B | 1 |
+----+------+---+
| 4 | B | 2 |
+----+------+---+
| 5 | B | 3 |
+----+------+---+
| 6 | A | 1 |
+----+------+---+
| 7 | A | 2 |
+----+------+---+
| 8 | B | 1 |
+----+------+---+
| 9 | B | 2 |
+----+------+---+
| 10 | B | 3 |
+----+------+---+
| 11 | A | 1 |
+----+------+---+
| 12 | B | 1 |
+----+------+---+
| 13 | B | 2 |
+----+------+---+
| 14 | A | 1 |
+----+------+---+
| 15 | A | 2 |
+----+------+---+
| 16 | B | 1 |
+----+------+---+
| 17 | C | 1 |
+----+------+---+
| 18 | C | 2 |
+----+------+---+
| 19 | A | 1 |
+----+------+---+
| 20 | A | 2 |
+----+------+---+
| 21 | B | 1 |
+----+------+---+
| 22 | B | 2 |
+----+------+---+
| 23 | C | 1 |
+----+------+---+
在上面的结果table中,一旦数据列在一行中发生变化,R值就会重置为1
更新 1
Ben Thul 的回答非常有效。
我建议在下面 post 更新参考这个答案。
T-sql Reset Row number on Field Change
这在文献中称为 "gaps and islands" 问题。首先,我提出的解决方案:
with cte as (
select *, [Id] - row_number() over (partition by [Col_Value] order by [Id]) as [GroupID]
from @table
)
select [Id], [Col_Value], row_number() over (partition by [GroupID], [Col_Value] order by [Id])
from cte
order by [Id];
为了说明,请注意,如果我使用 row_number()
枚举所有 "A" 值,那些连续的 row_number()
值将以与 ID 相同的速率上升价值。也就是说,他们的差异对于那个连续组(也称为 "island")中的那些人来说是相同的。一旦我们计算出该组标识符,只需枚举每个组的每个成员即可。