首页  ·  知识 ·  数据库
从最明细开始统计上面各级的汇总数
网友    MS-SQL  编辑:德仔   图片来源:网络
"color: #008080">-- "color: #008080">游戏规则:一颗树,从最明细开始统计上面各级的汇总数,不能用游标 st
--游戏规则:一颗树,从最明细开始统计上面各级的汇总数,不能用游标
--
建立测试数据
Declare @tmpTable Table(DocID varchar(20),ParentID varchar(20),Dep int,[Value] int)
Insert Into @tmpTable(DocID,ParentID,Dep,[Value])
Select '0001','0000',1,null
Union All Select '000101','0001',2,null
Union All Select '00010101','000101',3,null
Union All Select '0001010101','00010101',4,60
Union All Select '0001010102','00010101',4,40
Union All Select '00010102','000101',3,300
Union All Select '00010103','000101',3,200
Union All Select '00010104','000101',3,400
Union All Select '000102','0001',2,null
Union All Select '00010201','000102',3,500
Union All Select '000103','0001',2,null
Union All Select '00010301','000103',3,400
Union All Select '000104','0001',2,200
Select * From @tmpTable

--处理后结果
Declare @tmpReslut Table(DocID varchar(20),ParentID varchar(20),Dep int,[Value] int)
Insert Into @tmpReslut(DocID,ParentID,Dep,[Value])
Select '0001','0000',1,2100
Union All Select '0001
01','0001',2,1000
Union All Select '00010101','000101',3,100
Union All Select '0001010101','00010101',4,60
Union All Select '0001010102','00010101',4,40
Union All Select '00010102','000101',3,300
Union All Select '00010103','000101',3,200
Union All Select '00010104','000101',3,400
Union All Select '000102','0001',2,500
Union All Select '00010201','000102',3,500
Union All Select '000103','0001',2,400
Union All Select '00010301','000103',3,400
Union All Select '000104','0001',2,200
Select * From @tmpReslut
 
 
Declare @tmpTable Table(DocID varchar(20),ParentID varchar(20),Dep int,[Value] int)
Insert Into @tmpTable(DocID,ParentID,Dep,[Value])
Select '0001','0000',1,null
Union All Select '000101','0001',2,null
Union All Select '00010101','000101',3,null
Union All Select '0001010101','00010101',4,60
Union All Select '0001010102','00010101',4,40
Union All Select '00010102','000101',3,300
Union All Select '00010103','000101',3,200
Union All Select '00010104','000101',3,400
Union All Select '000102','0001',2,null
Union All Select '00010201','000102',3,500
Union All Select '000103','0001',2,null
Union All Select '00010301','000103',3,400
Union All Select '000104','0001',2,200
--Select * From @tmpTable
;with cte as(
select DocID,ParentID,Dep,[Value] from @tmpTable a where not exists(select 1 from @tmpTable where ParentID=a.DocID)
union all
select a.DocID,a.ParentID,a.Dep,[Value]=b.[Value]+isnull(a.[Value],0) from @tmpTable a inner join cte b on b.parentID=a.DocID
)
select DocID,ParentID,Dep,sum([Value])as [Value] from cte group by docid,parentid,dep
/*
DocID                ParentID             Dep         Value
-------------------- -------------------- ----------- -----------
0001                 0000                 1           2100
000101               0001                 2           1000
00010101             000101               3           100
0001010101           00010101             4           60
0001010102           00010101             4           40
00010102             000101               3           300
00010103             000101               3           200
00010104             000101               3           400
000102               0001                 2           500
00010201             000102               3           500
000103               0001                 2           400
00010301             000103               3           400
000104               0001                 2           200
*/
 
 
----------------------------------------------------------------------------------------------------------------------------
 
Declare @tmpTable Table(DocID varchar(20),ParentID varchar(20),Dep int,[Value] int)
Insert Into @tmpTable(DocID,ParentID,Dep,[Value])
Select '0001','0000',1,null
Union All Select '000101','0001',2,null
Union All Select '00010101','000101',3,null
Union All Select '0001010101','00010101',4,60
Union All Select '0001010102','00010101',4,40
Union All Select '00010102','000101',3,300
Union All Select '00010103','000101',3,200
Union All Select '00010104','000101',3,400
Union All Select '000102','0001',2,null
Union All Select '00010201','000102',3,500
Union All Select '000103','0001',2,null
Union All Select '00010301','000103',3,400
Union All Select '000104','0001',2,200
--Select * From @tmpTable

select docid,parentid,dep
        ,value
=(select sum(value) from @tmpTable where docid like a.docid+'%')
from @tmpTable a
/*
docid                parentid             dep         value
-------------------- -------------------- ----------- -----------
0001                 0000                 1           2100
000101               0001                 2           1000
00010101             000101               3           100
0001010101           00010101             4           60
0001010102           00010101             4           40
00010102             000101               3           300
00010103             000101               3           200
00010104             000101               3           400
000102               0001                 2           500
00010201             000102               3           500
000103               0001                 2           400
00010301             000103               3           400
000104               0001                 2           200
*/
-------------------------------------------------------------------------------------------
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读