27,580
社区成员
发帖
与我相关
我的任务
分享
create table #t(Account varchar(10),Sub int,Parent varchar(10),Value int)
insert into #t values('P0010', 0,'P0210',20)
insert into #t values('P0070', 0,'P0210',40)
insert into #t values('P0210',20,'P0290', 0)
insert into #t values('P0230', 0,'P0290',60)
insert into #t values('P0270', 0,NULL ,55)
insert into #t values('P0290',40,NULL , 0)
select identity(int,1,1) as id,tt.parent into #temp from (select distinct parent from #t where parent is not null) as tt
create table #f (Account varchar(10),Value int)
insert into #f
select Account,sum(Value)from #t where account not in (select parent from #t where parent is not null)group by account
declare @beginnum int
declare @endnum int
declare @P varchar(100)
set @beginnum=1
set @endnum = (select count(*)from #temp)
while @beginnum<=@endnum
begin
set @p=(select parent from #temp where id=@beginnum)
--set @beginnum=@beginnum+1
--print @p
--end
;WITH
t AS(
-- 定位点成员
SELECT * FROM #t
where account=@p
UNION ALL
-- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
SELECT A.*
FROM #t A, t B
WHERE A.parent = B.account
)
insert into #f
select @p,sum(value) from t
set @beginnum=@beginnum+1
end
select * from #f order by account
drop table #f
drop table #t
drop table #temp
GO
/*P0010 20
P0070 40
P0210 60
P0230 60
P0270 55
P0290 120*/
create table BOM(Account varchar(10),Sub int,Parent varchar(10),Value int)
insert into BOM values('P0010', 0,'P0210',20)
insert into BOM values('P0070', 0,'P0210',40)
insert into BOM values('P0210',20,'P0290', 0)
insert into BOM values('P0230', 0,'P0290',60)
insert into BOM values('P0270', 0,NULL ,55)
insert into BOM values('P0290',40,NULL , 0)
go
create function f_isChild(@Account1 varchar(10),@Account2 varchar(10))
returns int
as
begin
declare @t table(Account varchar(10),Sub int,Parent varchar(10),Value int)
insert into @t select * from BOM where Account=@Account1
while @@rowcount<>0
begin
if exists(select 1 from @t where Account=@Account2)
return 1
insert into @t
select
a.*
from
BOM a,@t b
where
a.Parent=b.Account and not exists(select 1 from @t where Account=a.Account)
end
return 0
end
go
select
a.Account,
Value=sum(case when dbo.f_isChild(a.Account,b.Account)=1 then b.Sub+b.Value else 0 end)
from
BOM a,BOM b
group by
a.Account
go
/*
Account Value
---------- -----------
P0010 20
P0070 40
P0210 80
P0230 60
P0270 55
P0290 180
*/
drop function f_ischild
drop table BOM
go
create table BOM(Account varchar(10),Sub int,Parent varchar(10),Value int)
insert into BOM values('P0010', 0,'P0210',20)
insert into BOM values('P0070', 0,'P0210',40)
insert into BOM values('P0210',20,'P0290', 0)
insert into BOM values('P0230', 0,'P0290',60)
insert into BOM values('P0270', 0,NULL ,55)
insert into BOM values('P0290',40,NULL , 0)
go
create function f_isChild(@Account1 varchar(10),@Account2 varchar(10))
returns int
as
begin
declare @t table(Account varchar(10),Sub int,Parent varchar(10),Value int)
insert into @t select * from BOM where Account=@Account1
while @@rowcount<>0
begin
insert into @t
select
a.*
from
BOM a,@t b
where
a.Parent=b.Account and not exists(select 1 from @t where Account=a.Account)
end
if exists(select 1 from @t where Account=@Account2)
return 1
return 0
end
go
select
a.Account,
Value=sum(case when dbo.f_isChild(a.Account,b.Account)=1 then b.Sub+b.Value else 0 end)
from
BOM a,BOM b
group by
a.Account
go
/*
Account Value
---------- -----------
P0010 20
P0070 40
P0210 80
P0230 60
P0270 55
P0290 180
*/
drop function f_ischild
drop table BOM
go