Windows機能をサポートするデータベースで使用します。 プロバイダごとに小さな変更が必要な場合があります。
デモ
create table SystemNote (Note char(7));
insert into SystemNote values
('R00000D'),('R00001D'),('R00002D'),('R00003D'),('R00004D')
,('R00012D'),('R00013D')
,('R00015D'),('R00016D'),('R00017D'),('R00018D')
,('R00021D')
,('R00025D'),('R00026D'),('R00027D')
select min (Note) as from_Note
,max (Note) as to_Note
,count (*) as Notes
from (select Note
,row_number() over (order by Note) as rn
from SystemNote
where Note like 'R%'
) sn
group by cast (substr(Note,2,5) as int) - rn
order by from_Note
+-----------+---------+-------+
| from_note | to_note | notes |
+-----------+---------+-------+
| R00000D | R00004D | 5 |
+-----------+---------+-------+
| R00012D | R00013D | 2 |
+-----------+---------+-------+
| R00015D | R00018D | 4 |
+-----------+---------+-------+
| R00021D | R00021D | 1 |
+-----------+---------+-------+
| R00025D | R00027D | 3 |
+-----------+---------+-------+
select dense_rank() over (order by cast (substr(Note,2,5) as int)-rn) as group_id
,row_number() over (partition by cast (substr(Note,2,5) as int)-rn order by note) as seq
,min (Note) over (partition by cast (substr(Note,2,5) as int)-rn) as from_Note
,max (Note) over (partition by cast (substr(Note,2,5) as int)-rn) as to_Note
,Note
from (select Note
,row_number() over (order by Note) as rn
from SystemNote
where Note like 'R%'
) sn
order by Note
+----------+-----+-----------+---------+---------+
| group_id | seq | from_note | to_note | note |
+----------+-----+-----------+---------+---------+
| 1 | 1 | R00000D | R00004D | R00000D |
+----------+-----+-----------+---------+---------+
| 1 | 2 | R00000D | R00004D | R00001D |
+----------+-----+-----------+---------+---------+
| 1 | 3 | R00000D | R00004D | R00002D |
+----------+-----+-----------+---------+---------+
| 1 | 4 | R00000D | R00004D | R00003D |
+----------+-----+-----------+---------+---------+
| 1 | 5 | R00000D | R00004D | R00004D |
+----------+-----+-----------+---------+---------+
| 2 | 1 | R00012D | R00013D | R00012D |
+----------+-----+-----------+---------+---------+
| 2 | 2 | R00012D | R00013D | R00013D |
+----------+-----+-----------+---------+---------+
| 3 | 1 | R00015D | R00018D | R00015D |
+----------+-----+-----------+---------+---------+
| 3 | 2 | R00015D | R00018D | R00016D |
+----------+-----+-----------+---------+---------+
| 3 | 3 | R00015D | R00018D | R00017D |
+----------+-----+-----------+---------+---------+
| 3 | 4 | R00015D | R00018D | R00018D |
+----------+-----+-----------+---------+---------+
| 4 | 1 | R00021D | R00021D | R00021D |
+----------+-----+-----------+---------+---------+
| 5 | 1 | R00025D | R00027D | R00025D |
+----------+-----+-----------+---------+---------+
| 5 | 2 | R00025D | R00027D | R00026D |
+----------+-----+-----------+---------+---------+
| 5 | 3 | R00025D | R00027D | R00027D |
+----------+-----+-----------+---------+---------+
こんにちは私はSQLサーバーを使用しています – DJR