【发布时间】:2021-12-26 09:27:17
【问题描述】:
我有一个跟踪变化的表。一列称为beforename,另一列称为aftername。
一些样本数据可能是:
| Parent | Child |
|---|---|
| a | b |
| b | c |
| c | d |
我正在尝试编写一个以返回更改的方式自我引用自身的查询,即:
a -> b -> c -> d (the arrows are just for notation here)
这可以在 SQL 中实现吗?
我的数据库是 SQL Server
【问题讨论】:
-
是的。使用recursive cte
-
我怀疑你需要一个合适的序列,比如日期时间或身份。
标签: sql sql-server tsql hierarchy chaining