Sql 将BOM结果返回到“中”;缩进;秩序

Sql 将BOM结果返回到“中”;缩进;秩序,sql,sql-server,Sql,Sql Server,我真的不需要缩进的结果,那只是我能想到的最好的标题。任何帮助都将不胜感激。我已经花了几个小时试图通过CTE做到这一点,这似乎是我要走的路,但我被卡住了 编辑:我可以按照Component_Job列对下面的示例数据进行排序,但是,在现实世界中,作业编号是随机的,不一定以任何可用顺序排列 我的表格包含以下内容: Root_Job Parent_Job Component_Job 1 1 1a 1 1 1b

我真的不需要缩进的结果,那只是我能想到的最好的标题。任何帮助都将不胜感激。我已经花了几个小时试图通过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
我正在尝试创建一个返回以下内容的视图:

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

这里可能有一些有用的东西:

declare @Jobs as Table ( ParentJob VarChar(10), ComponentJob VarChar(10) );
insert into @Jobs ( ParentJob, ComponentJob ) values
  ( '1', '1a' ), ( '1', '1b' ), ( '1', '1c' ),
  ( '1a', '1a1' ), ( '1a', '1a2' ), ( '1b', '1b1' ), ( '1b', '1b2' ),
  ( '2', '2a' ), ( '2', '2b' );

select * from @Jobs;

with Roots as (
  -- Find and fudge the root jobs.
  --   Usually they are represented as children without parents, but here they are implied by the presence of children.
  select distinct 1 as Depth, ParentJob as RootJob, Cast( ParentJob as VarChar(1024) ) as Path, ParentJob, ParentJob as ComponentJob
    from @Jobs as J
    where not exists ( select 42 from @Jobs where ComponentJob = J.ParentJob ) ),
  BoM as (
  -- Anchor the indented BoM at the roots.
  select Depth, RootJob, Path, ParentJob, ComponentJob
    from Roots
  union all
  -- Add the components one level at a time.
  select BoM.Depth + 1, BoM.RootJob, Cast( BoM.Path + '»' + J.ComponentJob as VarChar(1024) ), J.ParentJob, J.ComponentJob
    from BoM inner join
      @Jobs as J on J.ParentJob = BoM.ComponentJob )
  -- Show the result with indentation.
  select *, Space( Depth * 2 ) + ComponentJob as IndentedJob
    from BoM
    order by ComponentJob
    option ( MaxRecursion 0 );

在现实世界中,很少有事情能如此容易地分类。数字项(1,1.1,1.1.1,1.2)的诀窍是创建一个
路径
,将每个值零填充到固定长度,例如
0001
0001»0001
0001»0001»0001
0001»0002
,以便它们按字母顺序正确排序。您的数据可能会有所不同。

从工作清单中按组件选择*工作
我修改了上述内容以满足我的需要,并将订单更改为路径,效果非常好。谢谢