2

我有一个有趣的 SQL 问题。我有一个构成物料清单的零件层次结构表。与此类似:

ASSEMBLY
---------
parent_part_id
part_id
quantity

我通过这样的查询得到了这个结构的层次结构:

SELECT level, part_id, quantity
from assembly
start with parent_part_id = 1
connect by parent_part_id = prior part_id;

输出可能如下所示:

level  part_id  quantity
-----  -------  ---------
1      2        2
2      3        10
1      4        2
2      5        1    
3      3        5

到目前为止,一切都很好。

问题是这样的:我如何计算制作顶级装配所需的每个零件的总数(第 1 部分)?

按部件对结果集进行分组并对数量求和是不正确的,因为数量应乘以层次结构中当前部件正上方的部件数量,递归地向上遍历树。

我认为这是一个 LAG 功能,但无法将其可视化。

编辑:预期结果:

part_id  quantity
-------  --------
2        2
3        30
4        2
5        2

更多编辑:我通过这个查询得到了有趣的结果

SELECT rownum, level lvl, part_id, quantity, unit_of_measure
                , connect_by_isleaf || sys_connect_by_path(quantity,'*') math
            from assembly
            start with parent_part_id = 1
            connect by parent_part_id = prior part_id

数学列返回我要执行的计算的字符串表示形式:) 例如它可能会说:

1*1*2*10

或类似和适当的东西......也许制作一个函数来解析它并返回结果将解决问题......有人认为这很离谱吗?

4

2 回答 2

5

在 Oracle 11 R2 中,可以使用common table expression

测试数据:

--  drop table assembly;

create table assembly (
  part_id              number, 
  parent_part_id       number,
  quantity             number
);

insert into assembly values (2, 1,  2);
insert into assembly values (3, 2, 10);
insert into assembly values (4, 1,  2);
insert into assembly values (5, 4,  1);
insert into assembly values (3, 5,  5);

选择语句:

select 
  part_id, 
  sum(quantity_used) as quantity
from (
  with assembly_hier (lvl, part_id, quantity, quantity_used) as (
    select 
      1        lvl,
      part_id,
      quantity ,
      quantity        quantity_used
    from
      assembly
    where
      parent_part_id = 1 
  union all
    select
      assembly_hier.lvl      + 1 lvl,
      assembly     .part_id,
      assembly     .quantity,
      assembly_hier.quantity_used * assembly.quantity quantity_used
    from
      assembly_hier, assembly
    where
      assembly_hier.part_id = assembly.parent_part_id
  )
  select * from assembly_hier
)
group by part_id
order by part_id;

在 Ora11R2 之前编辑model clause,它可能适用于:

select 
  part_id,
  sum(quantity) quantity 
from (
  select
    lvl
    parent_part_id,
    part_id,
    quantity
  from (
    select 
      lvl,
      parent_part_id,
      part_id,
      quantity
    from (
      select  
        rownum r, 
        level lvl, 
        parent_part_id,
        part_id, 
        quantity
      from 
        assembly
      start with parent_part_id = 1
      connect by parent_part_id = prior part_id
    )
  )
  model
    dimension by (lvl, part_id)
    measures (quantity, parent_part_id)
    rules upsert (
       quantity[     any, any          ] order by lvl, part_id =   quantity[cv(lvl)  , cv(part_id)] * 
                                          nvl( quantity[cv(lvl)-1,    parent_part_id[cv(lvl), cv(part_id)] ], 1)
    )
)
group by part_id
order by part_id;

编辑二另一种可能性是将数量的对数相加,然后取和的指数:

select 
  part_id,
  sum(quantity) quantity
from (
  select 
    part_id,
    exp(sum(quantity_ln) over (partition by new_start order by r)) quantity
  from (
    select 
      r,
      lvl,
      part_id,
      quantity_ln,
      sum(new_start) over(order by r) new_start
    from (
      select 
        rownum r, 
        level lvl, 
        part_id, 
        ln(quantity) quantity_ln,
        nvl(lag(connect_by_isleaf,1) over (order by rownum),0) new_start
      from assembly
      start with parent_part_id = 1
      connect by parent_part_id = prior part_id
    )
  )
)
group by part_id
order by part_id
;
于 2011-01-24T20:14:23.673 回答
0

我在这里结束了:这适用于 oracle 10 和 11,connect_by_isleaf 可用于调整逻辑,无论您是只想对叶子求和,还是对所有节点求和。

select part_id,  new_rec.quantity*sum(math_calc( math,2)) m, unit_of_measure
from ( SELECT rownum, level lvl, part_id, quantity, unit_of_measure
            , connect_by_isleaf || sys_connect_by_path(quantity,'*') math
from assembly
start with parent_part_id = new_rec.part_id
connect by parent_part_id = prior part_id ) p
group by part_id, unit_of_measure 
于 2011-01-25T00:54:17.237 回答