使用 sql 的连续子集和排名

Continuous subset and ranking using sql

我有如下数据集:

现在,我需要 output 如下:

start_time  end_time    count
10:01   10:04   3
10:05   10:07   2 

为此,我写了一个查询,但它没有给我所需的序列。我的查询如下:

with on_off as 
(
select  time,status,case when status!=lag(status) over(order by time) then 1 else 0 end as continuous_count
  from time_status
)
,
grp as 
(
  select *, row_number() over(partition by continuous_count order by time) rnk from  on_off
)
select * from grp order by time  

它生成如下输出:

但是在 rank 部分我需要如下内容:

那么,我到底做错了什么?

这是 PostgresSQL DDL:

create table time_status(time varchar(10)  null, status varchar(10) null);

INSERT into  time_status(time,status) values('10:01','ON');
INSERT into  time_status(time,status) values('10:02','ON');
INSERT into  time_status(time,status) values('10:03','ON');
INSERT into  time_status(time,status) values('10:04','OFF');
INSERT into  time_status(time,status) values('10:05','ON');
INSERT into  time_status(time,status) values('10:06','ON');
INSERT into  time_status(time,status) values('10:07','OFF');

试试这个查询:

SELECT min(time) as start_time,
       max(time) as end_time,
       sum(case when status = 'ON' then 1 else 0 end) as cnt
  FROM (SELECT time, status,
               sum(case when status = 'OFF' then 1 else 0 end)
                 over (order by time desc) as grp
          FROM time_status) _
 GROUP BY grp
 ORDER BY min(time);

->Fiddle