帮助计算分层数据集中的复数总和
我有一个有趣的 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
或类似且适当的内容...也许可以创建一个函数解析这个并返回结果将解决问题..有人认为这很离谱吗?
I have an interesting SQL problem. I have a hierarchic table of parts that make a bill of material. similar to this:
ASSEMBLY
---------
parent_part_id
part_id
quantity
I get the hierarchy of this structure with a query like this:
SELECT level, part_id, quantity
from assembly
start with parent_part_id = 1
connect by parent_part_id = prior part_id;
the output might look like this:
level part_id quantity
----- ------- ---------
1 2 2
2 3 10
1 4 2
2 5 1
3 3 5
so far so good.
the question is this: how do I calculate the total number of each part required in order to make the top level assembly (part 1)?
Grouping this result set by part and summing the quantity is not correct, since the quantity should be multiplied by the quantity of the part immediately above the current part in the hierarchy, recursively up the tree.
I am thinking this is a LAG function, but having trouble visualizing it.
edit: expected results:
part_id quantity
------- --------
2 2
3 30
4 2
5 2
more edit: i get interesting results with this query
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
the math column returns a string representation of the calculation i want to perform :) for instance it may say:
1*1*2*10
or something similar and appropriate... perhaps making a function to parse this and return the result will solve the problem.. anyone think this is outrageous?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Oracle 11 R2 中,可以使用
公共表表达式
:测试数据:
select 语句:
编辑在 Ora11R2 之前,它可能与
模型子句一起使用< /code>:
编辑 II 另一种可能性是对数量的对数求和,然后取总和的指数:
In Oracle 11 R2 its possible with a
common table expression
:The test data:
The select statement:
Edit Prior to Ora11R2, it might work with a
model clause
:Edit II Another possibility would be to sum the logarithms of quantity and then take the sum's exponent:
我最终在这里:这适用于 oracle 10 和 11, connect_by_isleaf 可用于调整逻辑,无论您只想对叶子求和还是对所有节点求和。
i ended up here: this works on oracle 10 and 11, the connect_by_isleaf can be used to adjust the logic whether you want to sum only the leafs, or all nodes.