SQLITE 按序列汇总

SQLITE Summary by Sequence

我有一个 table 名称、开始时间和持续时间。
我需要一个能给我最后 5 个名字、开始时间和持续时间的查询。如果存在多个名称的序列 - 应该获取序列的开头及其整体延续。
如果有多个同名序列 - 应该只获取最后一个。

例如:

name start_time duration
a 79431 70
a 79420 11
a 79415 5
b 79413 1
c 79300 112
c 79298 2
b 79296 2
c 79288 6
c 79284 3
c 79278 347
d 78400 200
d 77764 636
e 77759 4
d 77741 17
f 77700 7
a 77642 10

应该是:

name seq start_time seq duration seq
a 79415 86
b 79413 1
c 79298 114
d 77764 836
e 77759 4

非常感谢帮助! 谢谢

当我们开始一个同名的新序列时,您需要定义。
我有 2 个查询,一个将所有出现的名称作为一个序列。如果他们跟随 table.

,另一个让他们在一起
create table table_name(
name varchar(10),
start_time int,
duration int);
insert into table_name values
('a',79431,70),
('a',79420,11),
('a',79415,5),
('b',79413,1),
('c',79300,112),
('c',79298,2),
('b',79296,2),
('c',79288,6),
('c',79284,3),
('c',79278,347),
('d',78400,200),
('d',77764,636),
('e',77759,4),
('d',77741,17),
('f',77700,7),
('a',77642,10);
select
  t.name,
  min(start_time) beginning,
  sum(duration) duration
from table_name t
join
(select distinct name
from table_name
order by start_time desc
limit 5) n
on t.name = n.name
group by t.name;
name | beginning | duration
:--- | --------: | -------:
a    |     77642 |       96
b    |     79296 |        3
c    |     79278 |      470
d    |     77741 |      853
e    |     77759 |        4
select
name,
min(start_time) beginning,
sum(duration) duration
from
(select 
  name,
  start_time,
  duration,
  row_number() over(order by start_time) rn,
  row_number() over(order by start_time desc)
   - row_number() over(partition by name order by start_time desc) as group_number
from table_name) t
group by name,group_number
order by group_number
limit 5;
name | beginning | duration
:--- | --------: | -------:
a    |     79415 |       86
b    |     79413 |        1
c    |     79298 |      114
b    |     79296 |        2
c    |     79278 |      356

db<>fiddle here