SQL问题,请问结构菜单计算问题?

cch1010 2008-01-22 10:54:23
表结构如下:

结构序号 物品编号 物品类型 物品名称 父件编号 物品用量
1 001 产品 A NULL NULL
2 002 产品 B NULL NULL
3 003 零部件 C 001 3
4 004 零部件 D 001 2
5 005 原材料 E 003 4
6 006 原材料 F 003 3
7 007 原材料 G 004 5
8 003 零部件 C 002 4
9 009 零部件 H 002 3
10 005 原材料 E 009 1
11 006 原材料 F 009 1
请问:如何SQL查询父编号之下的物品总用量
例如:按001查时需查到
003 C 3个
004 D 2个
005 E 4*3个
006 F 3*3个
007 G 5*2个
按003查询时需查到
005 E 4个
006 F 3个

...全文
110 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cch1010 2008-01-22
  • 打赏
  • 举报
回复
多谢大家!!
wzy_love_sly 2008-01-22
  • 打赏
  • 举报
回复
漫步真勤快
pt1314917 2008-01-22
  • 打赏
  • 举报
回复

create table tb(结构序号 int,物品编号 varchar(50),物品类型 varchar(50),
物品名称 varchar(50),父件编号 varchar(50),物品用量 int)
insert into tb select 1,'001','产品','A','',0
insert into tb select 2,'002','产品','B','',0
insert into tb select 3,'003','零部件','C','001',3
insert into tb select 4,'004','零部件','D','001',2
insert into tb select 5,'005','原材料','E','003',4
insert into tb select 6,'006','原材料','F','003',3
insert into tb select 7,'007','原材料','G','004',5
insert into tb select 8,'003','零部件','C','002',4
insert into tb select 9,'009','零部件','H','002',3
insert into tb select 10,'005','原材料','E','009',1
insert into tb select 11,'006','原材料','F','009',1

create function wsp_a(@物品编号 varchar(50))
returns @t table(物品编号 varchar(50),物品名称 VARCHAR(50),父件编号 VARCHAR(50),物品用量 int,Level int)
as
begin
declare @i int
set @i=1
insert into @t select 物品编号,物品名称,父件编号,物品用量,@i from tb where 父件编号=@物品编号
while @@rowcount<>0
begin
set @i=@i + 1
insert into @t select a.物品编号,a.物品名称,a.物品用量,a.父件编号,@i from tb a,@t b
where a.父件编号=b.物品编号 and b.Level=@i-1
end
return
end

-----查询'001'时
select 物品编号,物品名称,物品用量=case 父件编号 when '001' then ltrim(物品用量)+'个'
else 父件编号+'*'+ltrim(物品用量)+'个' end from dbo.wsp_a('001')

-----查询'003'时
select 物品编号,物品名称,物品用量=case 父件编号 when '003' then ltrim(物品用量)+'个'
else 父件编号+'*'+ltrim(物品用量)+'个' end from dbo.wsp_a('003')
wzy_love_sly 2008-01-22
  • 打赏
  • 举报
回复
对啊,数据都存函数里了,你只要group by就出来了
cch1010 2008-01-22
  • 打赏
  • 举报
回复
谢谢老乌龟,但这个需要求父节点下的各个子节点的总用量
昵称被占用了 2008-01-22
  • 打赏
  • 举报
回复
凭感觉写,未测试,估计问题不大,有问题自己改吧

昵称被占用了 2008-01-22
  • 打赏
  • 举报
回复

--写个函数
create function dbo.fn_GetChild(
@物品编号 varchar(10)
)
returns @r table (
物品编号 varchar(10),
物品名称 varchar(100),
物品用量 int,
层次 int
)
as
begin
declare @l int
set @l=0
insert @r select
物品编号,
物品名称,
物品用量,
层次=0
from Tab
where 父件编号=@物品编号
while exists (select 1
from Tab a,@r b
where a.父件编号=b.物品编号
and b.层次=@l
)
begin
insert @r select
a.物品编号,
a.物品名称,
a.物品用量*b.物品用量 as 物品用量,
层次=@l+1
from Tab a,@r b
where a.父件编号=b.物品编号
and b.层次=@l
set @l=@l+1
end
return
end
go

--调用
select
物品编号,
物品名称,
物品用量
from dbo.fn_GetChild('001')
go
select
物品编号,
物品名称,
物品用量
from dbo.fn_GetChild('003')
go

dawugui 2008-01-22
  • 打赏
  • 举报
回复
--这里还有个类似的.
CREATE TABLE BOM(PID INT,ID INT)
INSERT INTO BOM SELECT 801,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GO

CREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
DECLARE @ID INT
WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
BEGIN
SET @ID=@PID
SELECT @PID=PID FROM BOM WHERE ID=@ID
END
RETURN @PID
END
GO

SELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO

/*
PID ID
----------- -----------
801 101
801 102
801 103
801 601
801 101
801 105
801 501
801 106
801 121
*/


DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO




--生成测试数据
create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20))
insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
insert into BOM_1 select 3 ,'A2','A3',3,'生产'
insert into BOM_1 select 4 ,'A2','A4',2,'采购'
insert into BOM_1 select 5 ,'A3','A5',2,'采购'
insert into BOM_1 select 6 ,'A3','A6',1,'采购'
insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
insert into BOM_1 select 9 ,'B2','B3',3,'生产'
insert into BOM_1 select 10,'B2','B4',2,'采购'
insert into BOM_1 select 11,'B3','B5',2,'采购'
insert into BOM_1 select 12,'B3','B6',2,'采购'
go


--创建用户定义函数,用于取每个父节点下子节点的采购配置信息
create function f_stock(@bom_head varchar(20))
returns @t table(bom varchar(20),number int)
as
begin
declare @level int
declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
set @level=1

if exists(select 1 from BOM_1 where bom_head=@bom_head)
insert into @a
select bom_child,number,products_attribute,@level
from BOM_1
where bom_head=@bom_head

while exists(select 1 from @a where [level]=@level and products_attribute='生产')
begin
set @level=@level+1
insert into @a(bom,number,products_attribute,[level])
select a.bom_child,a.number,a.products_attribute,@level
from BOM_1 a,@a b
where a.bom_head=b.bom and b.[level]=@level-1
end

insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
return
end
go


--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')






--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go

--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go

--执行查询
select ID from dbo.f_getChild(3)
go

--输出结果
/*
ID
----
5
6
7
*/

--删除测试数据
drop function f_getChild
drop table BOM
dawugui 2008-01-22
  • 打赏
  • 举报
回复
--bom成本计算?
表一:品号信息档
品号 品号属性 材料成本 人工成本 本阶人工
MB001 MB025 MB057 MB058 MB061
A M 2
B M 1
C P 2
D P 3
(MB025=M 时只有本阶人工是基础数据,其他的是计算过来的,MB025=P时只有MB057有基础数据)


表二(BOM)
主件品号 元件品号 用量
A B 2(A由2个B组成)

B C 1 (B由1个C和3个D组成)
B D 3

计算原理:
1、计算MB025=M 的人工成本,人工成本=本阶人工+下阶人工成本
2、计算材料成本,材料成本=本阶用到材料+下阶材料之和(如本例

结果:
MB001 MB025 MB057 MB058 MB061
A M 22 4 2
B M 11 1 1
C P 2 0 0
D P 3 0 0

11=(3D+1C)=11
22=11*2
4=2B+2










go
create table BOM(主件品号 nvarchar(2),元件品号 nvarchar(2), 用量 int)
insert BOM select 'A','B',2
insert BOM select 'B','C',1
insert BOM select 'B','D',3
go
create table product(MB001 nvarchar(2), MB025 nvarchar(2), MB057 int, MB058 int, MB061 int)
insert product select 'A','M',null,null, 2
insert product select 'B','M',null,null, 3-----改为3测试
insert product select 'C','P', 2 ,null,null
insert product select 'D','P', 3 ,null,null
go

go
create function BomTree(@Product nvarchar(2))
returns numeric(18,5)
as
begin

declare @T table(主件品号 nvarchar(2),元件品号 nvarchar(2), 用量 int,lev int)
declare @i int,@num numeric(18,5)
if not exists(select 1 from BOM where 主件品号=@Product)
begin
select
@num=sum(MB057)
from
product
where
MB001=@Product
return @num
end

set @i=0
insert @T select 主件品号,元件品号,用量,@i from BOM where 主件品号=@Product
while @@rowcount>0
begin
set @i=@i+1
insert @T
select
t2.主件品号,t2.元件品号,t.用量*t2.用量,@i
from @t t join BOM t2 on t.元件品号=t2.主件品号
where t.Lev=@i-1
end
select
@num=sum(t.用量*case when t2.MB057>0 then t2.MB057 else 1 end)
from
@t t
join
product t2 on t.元件品号=t2.MB001
where
not exists(select 1 from @t where t.元件品号=主件品号)

return @num
end
go

create function BomTree2(@Product nvarchar(2))
returns numeric(18,5)
as
begin

declare @T table(主件品号 nvarchar(2),元件品号 nvarchar(2), 用量 int,lev int)
declare @i int,@num numeric(18,5)
if not exists(select 1 from BOM t where 主件品号=@Product
and not exists(select 1 from product where MB001=t.元件品号 and isnull(MB061,0)!>0))
begin
select
@num=sum(isnull(MB061,0))
from
product
where
MB001=@Product
return @num
end

set @i=0
insert @T select 主件品号,元件品号,用量,@i from BOM where 主件品号=@Product
while @@rowcount>0
begin
set @i=@i+1
insert @T
select
t2.主件品号,t2.元件品号,t.用量*t2.用量,@i
from @t t join BOM t2 on t.元件品号=t2.主件品号
where t.Lev=@i-1
end
select
@num=sum(t.用量*isnull(t2.MB061,1))
from
@t t
join
product t2 on t.元件品号=t2.MB001------改一下判断

where
not exists(select 1 from product where MB001=t.元件品号 and isnull(MB061,0)!>0)


return @num
end
go

select
MB001, MB025 , [MB057]=dbo.BomTree(MB001), [MB058]=isnull([MB061],0)+dbo.BomTree2(MB001), [MB061]=isnull([MB061],0)

from
product
go
--drop table product,BOM
--drop function BomTree,BomTree2

MB001 MB025 MB057 MB058 MB061
----- ----- -------------------- --------------------- -----------
A M 22.00000 8.00000 2
B M 11.00000 6.00000 3
C P 2.00000 .00000 0
D P 3.00000 .00000 0

(所影响的行数为 4 行)

dawugui 2008-01-22
  • 打赏
  • 举报
回复
BOM?
cch1010 2008-01-22
  • 打赏
  • 举报
回复
漫步,你写的好像有点不妥哦
结果分别是:
001:
003 C 3个
004 D 2个
005 E 4*3个
006 F 3*3个
007 G 5*4个(5*2个)

003:
005 E 4个
006 F 3个

按''(产品)查询时
001 A 1个
002 B 1个
003 C 3*1个
004 D 2*1个
003 C 4*2个
009 H 3*2个
005 E 4*3个
005 E 4*3个(4*4)
006 F 3*3个
006 F 3*3个(3*4)
007 G 5*4个(5*2)
005 E 1*9个(1*3)
006 F 1*9个(1*3)
pt1314917 2008-01-22
  • 打赏
  • 举报
回复
好了就结帖吧。。。

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧