我先尝试使用物化路径和hierarchyid查询BOM的方法。但结果仍然不正确。 ItemNumber / 1/5 /与qty 3的结果是38.12,这个结果必须是114.36因为乘以qty。有人可以解释一下如何解决这个问题。
以下是代码:
declare @BOMStructure as table
(
PartNumber varchar(14)not null ,
Descript varchar(50)not null,
Qty integer not null default 0,
Price Decimal (10,2) default 0,
ItemNumber hierarchyid not null primary key
)
INSERT @BOMStructure
(PartNumber ,Descript ,Qty ,Price ,ItemNumber)
VALUES ('13400201001','MAIN ASSEMBLY',1,0,'/'),
('00150060060005','BASIC TANK',1,0,'/1/'),
('11012142200503','SHELL',1,789.89,'/1/1/'),
('12052140503','TOP CONE',1,226.75,'/1/2/'),
('13052140503','BOTTOM CONE',1,226.75,'/1/3/'),
('140104116508','PIPE LEG',3,39.75,'/1/4/'),
('15004104','BALL FEET',3,0,'/1/5/'),
('1510413504','SLEEVE',1,18.03,'/1/5/1/'),
('1524809510','ADJUSTABLE BOLT',1,12.82,'/1/5/2/'),
('1530411604','BASE',1,7.27,'/1/5/3/')
-- GetAncestor
-- Mengupdate
select PartNumber, Descript,Qty,Price, (select sum (Price * Qty)
from @BOMStructure where ItemNumber .IsDescendantOf (p.ItemNumber ) = 1 ) as [TotalPrice],
ItemNumber .ToString() as [Hierarcy], ItemNumber .GetLevel() as [Level]
from @BOMStructure as P;
答案 0 :(得分:1)
您可以乘以p.qty
:
SELECT partnumber, descript, qty, price,
p.qty * (SELECT Sum(price * qty)
FROM bomstructure
WHERE itemnumber.Isdescendantof(p.itemnumber) = 1
) as [TotalPrice],
itemnumber.Tostring() as [Hierarcy],
itemnumber.Getlevel() as [Level]
FROM bomstructure as P;
但是,我并非100%确定这可以满足您所有行的需要。
Here是一个SQL小提琴。