我相信好多程式設計師朋友都遇到過核算公司bom,求乙個成品的單價的問題。在csdn上,也有許多朋友發貼問這樣的問題。我為我們公司開發了許多報表,涉及到各個部門,當然也包括採購部門(採購經理要看),那核算bom成品單價也是理所當然的事了。其實核算乙個bom成品的演算法不是很難,但要以樹型的方式來顯示就有點難度了。在我們公司,我寫了兩個報表乙個是單層的(老闆要看),另乙個是多層的也就是樹型結構(工程部,採購部要看)。老闆看單層,是因為他只關心結果,只要看乙個成品下面第一層每個半成品共要多少錢就可以了。而工程部可不同了,他們可要看到乙個層次結構。下面我以這個bom為例,詳細敘述如何核算bom及層次顯示。follow me!
fg001
sfg001 sfg002 sfg003
wip001 wip002 wip003 wip004 wip005 wip006
raw001 raw002 raw003 raw004,raw005 ............
kkk001 kkk003
www005
一:先建立bom表
create table t
(parent varchar(10),
child varchar(10),qty numeric(9,2)
)insert into t
select 'fg001', 'sfg001', 1 union all
select 'fg001' , 'sfg002', 1 union all
select 'fg001' ,'sfg003', 1 union all
select 'sfg001', 'wip001', 2 union all
select 'sfg001' ,'wip002', 2 union all
select 'sfg002' ,'wip003', 3 union all
select 'sfg002' ,'wip004', 3 union all
select 'sfg002' ,'wip005', 2 union all
select 'sfg003' ,'wip006', 3 union all
select 'wip001' ,'raw001', 2.66 union all
select 'wip001' ,'raw002' , 2.33 union all
select 'wip002' ,'raw003' , 3.21 union all
select 'wip003' ,'raw004' , 1.89 union all
select 'wip003' ,'raw005' , 1.86 union all
select 'raw001','kkk001', 3.25 union all
select 'raw004','kkk003', 4.26 union all
select 'kkk001','www005', 5.23
二:建立函式(a:樹型結構顯示)
create function test(@parent varchar(10))
returns @t table(parent nvarchar(10),child nvarchar(10),qty numeric(9,2),
level int,sort nvarchar(1000)collate latin1_general_bin )
asbegin
declare @level int
set @level=1
insert into @t
select parent,child,qty,@level,parent+child
from t
where parent=@parent collate latin1_general_bin
while @@rowcount>0
begin
set @level=@level+1
insert @t
select a.parent,a.child,a.qty*b.qty,@level,b.sort+a.child
from t a ,@t b
where a.parent=b.child collate latin1_general_bin
and b.level=@level-1
endreturn
end--呼叫函式
select
level,
space(level*2)+'|--' + child as 'product',
qtyfrom
dbo.test('fg001')
order by
sort
--結果
/**//*
level product qty
1 |--sfg001 1.00
2 |--wip001 2.00
3 |--raw001 5.32
4 |--kkk001 17.29
5 |--www005 90.43
3 |--raw002 4.66
2 |--wip002 2.00
3 |--raw003 6.42
1 |--sfg002 1.00
2 |--wip003 3.00
3 |--raw004 5.67
4 |--kkk003 24.15
3 |--raw005 5.58
2 |--wip004 3.00
2 |--wip005 2.00
1 |--sfg003 1.00
2 |--wip006 3.00
(17 row(s) affected)
*/三:建立函式(b:單層結構顯示)
create function test(@parent varchar(10))
returns @t table(parent nvarchar(10),child nvarchar(10),qty numeric(9,2),
level int,sort nvarchar(1000)collate latin1_general_bin )
asbegin
declare @level int
set @level=1
insert into @t
select parent,child,qty,@level,parent
from t
where parent=@parent collate latin1_general_bin
while @@rowcount>0
begin
set @level=@level+1
if @level=2
begin
insert @t
select a.parent,a.child,a.qty*b.qty,@level,a.parent
from t a ,@t b
where a.parent=b.child collate latin1_general_bin
and b.level=@level-1
endelse
begin
insert @t
select a.parent,a.child,a.qty*b.qty,@level,b.sort
from t a ,@t b
where a.parent=b.child collate latin1_general_bin
and b.level=@level-1
endend
return
end--函式呼叫
select * from dbo.test('fg001')
BOM資料排序及分級顯示
bom資料排序及分級顯示 示例資料 create table bomsub parent item varchar 10 sub item varchar 10 insert bomsub select a aa union all select a ab union all select aa a...
BOM資料排序及分級顯示
bom資料排序及分級顯示 示例資料 create table bomsub parent item varchar 10 sub item varchar 10 insert bomsub select a aa union all select a ab union all select aa a...
BOM資料排序及分級顯示
bom資料排序及分級顯示 示例資料 create table bomsub parent item varchar 10 sub item varchar 10 insert bomsub select a aa union all select a ab union all select aa a...