我真的不需要缩进的结果,这只是我能想到的最好的标题。非常感激任何的帮助。我花了几个小时试图通过 CTE 做到这一点,这似乎是要走的路,但我被困住了。
编辑:我可以按 Component_Job 列对下面的示例数据进行排序,但是,在现实世界中,作业编号是随机的,不一定按任何可用顺序。
我的表包含以下内容:
Root_Job Parent_Job Component_Job
1 1 1a
1 1 1b
1 1 1c
1 1a 1a1
1 1a 1a2
1 1b 1b1
1 1b 1b2
2 2 2a
2 2 2b
我正在尝试创建一个返回以下内容的视图:
Root_Job Parent_Job Component_Job
1 1 1a
1 1a 1a1
1 1a 1a2
1 1 1b
1 1b 1b1
1 1b 1b2
1 1 1c
2 2 2a
2 2 2b
只是为了澄清我想要实现的退货单是:
1
1a
1a1
1a2
1b
1b1
1b2
1c
2
2a
2b
最后,我一直在尝试但对我无能为力的 CTE 是:
with BOM (Root_job, parent_job, component_Job)
as
(
-- Anchor member definition
SELECT e.Root_Job, e.Parent_Job, e.Component_Job
FROM Bill_Of_Jobs AS e
WHERE Root_Job = Parent_Job
UNION ALL
-- Recursive member definition
SELECT e.Root_Job, e.Parent_Job, e.Component_Job
FROM Bill_Of_Jobs AS e
INNER JOIN bill_of_Jobs AS d
ON e.parent_Job = d.Component_Job
)
-- Statement that executes the CTE
SELECT * from BOM