我正在尝试在SQL中使用行号。但是,它没有提供所需的输出。
数据:
ID Name Output should be
111 A 1
111 B 2
111 C 3
111 C 3
111 A 4
222 A 1
222 A 1
222 B 2
222 C 3
222 B 4
222 B 4
这是一个差距和孤岛问题。首先:为了使问题有意义,您需要一个定义行顺序的列 - 我假设ordering_id
.然后,我建议lag()
获取"上一个"name
,以及每次相邻行中name
发生变化时增加的累积sum()
:
select id, name,
sum(case when name = lag_name then 0 else 1 end) over(partition by id order by ordering_id) as rn
from (
select t.*, lag(name) over(partition by id order by ordering_id) lag_name
from mytable t
) t
SQL Server 2008使这变得更加棘手。 您可以使用行号的差异来标识相邻的行。 然后,您可以在每个岛屿中分配最小 ID 并使用dense_rank()
:
select t.*,
dense_rank() over (partition by name order by min_ordcol) as output
from (select t.*,
min(<ordcol>) over (partition by name, seqnum - seqnum_2) as min_ordcol
from (select t.*,
row_number() over (partition by name order by <ordcol>) as seqnum,
row_number() over (partition by name, id order by <ordcol>) as seqnum_2
from t
) t
) t;