34,587
社区成员
发帖
与我相关
我的任务
分享
if not object_id('A') is null
drop table A
Go
Create table A([Account] nvarchar(5),[Sub] int,[Parent] nvarchar(5),[Value] int)
Insert A
select N'P0010',0,N'P0210',20 union all
select N'P0070',0,N'P0210',40 union all
select N'P0210',20,N'P0290',0 union all
select N'P0230',0,N'P0290',60 union all
select N'P0270',0,null,55 union all
select N'P0290',40,null,0
Go
create function F_Value(@Account nvarchar(5))
returns int
as
begin
declare @Value int
select @Value=[Value] from A where [Account]=@Account and [Sub]=0;
if @Value is not null
return @Value
;with C as
(select * from A where [Account]=@Account
union all
select a.* from C join A on C.[Account]=a.[Parent])
select @Value=sum([Value]) from C
return @Value
end
go
select
[Account],
[Value]=dbo.F_Value([Account])
from
A
Account Value
------- -----------
P0010 20
P0070 40
P0210 60
P0230 60
P0270 55
P0290 120
(6 個資料列受到影響)
if not object_id('A') is null
drop table A
Go
Create table A([Account] nvarchar(5),[Sub] int,[Parent] nvarchar(5),[Value] int)
Insert A
select N'P0010',0,N'P0210',20 union all
select N'P0070',0,N'P0210',40 union all
select N'P0210',20,N'P0290',0 union all
select N'P0230',0,N'P0290',60 union all
select N'P0270',0,null,55 union all
select N'P0290',40,null,0
Go
;with CTE as
(select *,[Account] as Name from A where [Sub]<>0 and [Value] is not null
union all
select a.*,c.Name from Cte c join A on c.[Account]=a.[Parent])
select
a.[Account],[Value]=isnull(b.[Value],a.[Value])
from
a
left join
(select Name as [Account],sum([Value])[Value] from CTE group by Name) b on a.[Account]=b.[Account]
option(MAXRECURSION 0)
(6 個資料列受到影響)
Account Value
------- -----------
P0010 20
P0070 40
P0210 60
P0230 60
P0270 55
P0290 120
(6 個資料列受到影響)
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 test(account varchar(10), sub int,parent varchar(10), value int)
insert into test select 'P0010', 0, 'P0210', 20
insert into test select 'P0070', 0, 'P0210', 40
insert into test select 'P0210',20, 'P0290', 0
insert into test select 'P0230', 0, 'P0290', 60
insert into test select 'P0270', 0, '', 55
insert into test select 'P0290', 40, '', 0
GO
select * into #test from test
while @@rowcount>0
begin
update #test
set value=value+(select sum(value) from #test a where parent=#test.account)
, sub=0
where sub>0
and not exists(select 1 from #test b where parent=#test.account and sub>0)
end
select account,value from #test
/*
account value
---------- -----------
P0010 20
P0070 40
P0210 60
P0230 60
P0270 55
P0290 120
*/
GO
drop table #test,test