作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] Oracle connect by 语法,数量累乘
时间Wed Jul 29 15:28:52 2015
MSSQL 解法:
-- 你的资料表
create table #temp(
mid char(1), -- 主料号
cid char(1), -- 子料号
qty integer, -- 用量
mul integer, -- 累计用量(相乘)
)
-- 测资
insert into #temp select 'A','B',2,NULL
insert into #temp select 'B','C',3,NULL
-- 回圈 propagate
while 1 = 1 begin
update t1
set mul = t2.mul
from #temp t1 inner join (
select
t1.mid,
case when t2.mid is null then t1.qty else t1.qty * t2.mul end mul
from #temp t1 left join #temp t2
on t1.mid = t2.cid
) t2 on t1.mul is null and t1.mid = t2.mid
if @@ROWCOUNT = 0 break
end
-- 显示结果(只秀出 leaf 结果)
select t1.cid,t1.mul
from #temp t1 left join #temp t2
on t1.cid = t2.mid
where t2.mid is null
※ 引述《bohei (run and fall)》之铭言:
: 大家好
: 目前正在做一个BOM的展开
: 使用的是connecy by prior 语法 目前唯独QPA无法算出
: 想用累乘的概念把用量算出来 下面是简单的例子
: 主料号 子料号 用量
: ====================
: A B 2
: B C 3
: SELECT 子料号,用量 FROM table
: WHERE connect_by_isleaf=1
: START WITH 主料号 = 'A'
: CONNECT BY PRIOR 子料号 = 主料号
: 希望最後出来的结果是
: 子料号 用量
: ============
: C 6
: 不知道有没有办法做到? 先谢谢大家!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.221.80.36
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1438154934.A.C7A.html