一步一步来呗:
WITH A AS(
SELECT 23 AS ID,'1A' AS TYPE FROM DUAL
UNION ALL
SELECT 24 AS ID,'1A' AS TYPE FROM DUAL
UNION ALL
SELECT 26 AS ID,'2A' AS TYPE FROM DUAL
UNION ALL
SELECT 27 AS ID,'2A' AS TYPE FROM DUAL
UNION ALL
SELECT 28 AS ID,'2A' AS TYPE FROM DUAL
UNION ALL
SELECT 31 AS ID,'2A' AS TYPE FROM DUAL
UNION ALL
SELECT 32 AS ID,'2A' AS TYPE FROM DUAL
),
B AS (
select id,type,dense_rank() OVER(ORDER BY ID-ROWNUM) DD from A
),
C AS(
select type,dd,min(id) xx,max(id) mm
from b
group by type,dd)
SELECT type as "类型",xx as "开始序号",mm as "结束序号",(mm-xx+1) as "连续次数"
FROM C
ORDER BY type