视图有问题,我正在使用Server。我有一张这样的桌子:
+-------+------+
| Start | End |
+-------+------+
| 1 | Null |
| 3 | 4 |
| 6 | 9 |
+-------+------+这个表表示一系列的时间框架,如果End is Null --这意味着它还没有完成,但是可能会有短时间间隔(3-4和6-9),我想创建一个视图来显示如下所有的时间框架:
+-------+------+
| Start | End |
+-------+------+
| 1 | 3 |
| 3 | 4 |
| 4 | 6 |
| 6 | 9 |
| 9 | Null |
+-------+------+我找不到解决办法。我试了一个多小时却没有结果。
发布于 2016-10-23 15:44:16
我想你想要union all和lead()
select start, lead(start) over (order by start)
from ((select t.start as start from likethis t
) union all
(select t.end from likethis t
)
) t
where start is not null
order by start;在Server的早期版本中,可以使用cross apply
with t as (
select t.start as start from likethis t
union all
select t.end from likethis t
)
select t.start, tnext.start
from t cross apply
(select top 1 t2.*
from t t2
where t2.start > t.start
order by t2.start desc
) tnext
order by start;https://stackoverflow.com/questions/40204690
复制相似问题