Re: [SQL ] Oracle connect by 語法,數量累乘
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://www.ptt.cc/bbs/Database/M.1438154934.A.C7A.html
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 5 篇):