我有一张类似的表:
jobno | startno | endno |
---|---|---|
123 | 1 | 5 |
456 | 6 | 7 |
789 | 8 | 10 |
您想要递归cte
:
with cte as (
select jobno, startno, endno
from table t
union all
select jobno, startno + 1, endno
from cte c
where startno < endno
)
select c.startno, c.jobno
from cte c
order by c.jobno, c.startno;
这是假设您正在使用SQL Server
运行,则语法可能会有所不同。
如果startno
具有更多差距,请使用option (maxrecursion 0)
。
您没有指定您的DBM,但是对于Postgres,您可以使用generate_series()
:
select x.i as startno, j.jobno
from jobs j
join generate_series(j.startno, j.endno) as x(i) on true;
在线示例:https://rextester.com/rzn4872