找出 Table 中连续出现次数最多的值
Find the Biggest Number of Consecutive Occurrence of values in Table
我有以下table
create table Launches (Id int, Name char)
insert into Launches values
(1, 'A'),
(2, 'A'),
(3, 'B'),
(4, 'B'),
(5, 'B'),
(6, 'B'),
(7, 'C'),
(8, 'B'),
(9, 'B')
结果应该是
4 - B
从 3 到 6
类似问题-
您可以为每个 name
减去一个枚举值以获得相同的相邻值的常数。剩下的就是聚合:
select top (1) name, count(*), min(id), max(id)
from (select l.*,
row_number() over (partition by name order by id) as seqnum
from #Launches l
) l
group by (id - seqnum), name
order by count(*) desc;
Here 是一个 db<>fiddle.
我有以下table
create table Launches (Id int, Name char)
insert into Launches values
(1, 'A'),
(2, 'A'),
(3, 'B'),
(4, 'B'),
(5, 'B'),
(6, 'B'),
(7, 'C'),
(8, 'B'),
(9, 'B')
结果应该是
4 - B
从 3 到 6
类似问题-
您可以为每个 name
减去一个枚举值以获得相同的相邻值的常数。剩下的就是聚合:
select top (1) name, count(*), min(id), max(id)
from (select l.*,
row_number() over (partition by name order by id) as seqnum
from #Launches l
) l
group by (id - seqnum), name
order by count(*) desc;
Here 是一个 db<>fiddle.