当前位置: 代码迷 >> Sql Server >> 怎么实现递归汇总
  详细解决方案

怎么实现递归汇总

热度:94   发布时间:2016-04-24 08:47:00.0
如何实现递归汇总


ID                      mc           parent      sl
1             其他应付        -1            0
101             社保金        1           0 
102             社保金        1           5 
10101         社保金A         101        12
10102         社保金A         101        13

但sl应该通过parent进行累计,如何累计得到

ID                      mc           parent      sl
1             其他应付        -1            30
101             社保金        1           25 
10101         社保金A         101        12
10102         社保金A         101        13
102             社保金        1           5 
------解决思路----------------------
用CTE
http://blog.vsharing.com/janezhangxy/A1655658.html
这个例子跟你的很类似。
------解决思路----------------------
--假设表T
with cte as (select id as PId,* from T union all
         select a.PId,T.ID,T.mc,T.parent,T.sl from T,cte a where a.ID=T.parent)
         
select t.ID,T.mc,T.parent,SUM(a.sl) sl from cte a,T where a.PId=t.id  group by t.ID,T.mc,T.parent
 
------解决思路----------------------

-- 如果你的 ID 是这样有规律的话,

create table test (id varchar(10) , mc varchar(10) , parent varchar(10),sl int)
go
insert into test(id , mc , parent,sl) values
(1,  '其他应付' , '-1' , 0),
(101, '社保金' , '1' , 0) ,
(102, '社保金'  , '1' , 5 ),
(10101,'社保金A' , '101' , 12),
(10102,'社保金A' , '101',  13)
go
select * from test 
go
select t.id , t.mc, t.parent,
(select SUM(sl) from test where id like  t.id + '%') 
from test t
order by 1
go
drop table test 
go


(5 行受影响)
id         mc         parent     sl
---------- ---------- ---------- -----------
1          其他应付       -1         0
101        社保金        1          0
102        社保金        1          5
10101      社保金A       101        12
10102      社保金A       101        13

(5 行受影响)

id         mc         parent     
---------- ---------- ---------- -----------
1          其他应付       -1         30
101        社保金        1          25
10101      社保金A       101        12
10102      社保金A       101        13
102        社保金        1          5

(5 行受影响)



------解决思路----------------------

-- 那就先取出所有的 末级行 

create table test (id varchar(10) , mc varchar(10) , parent varchar(10),sl int)
go
insert into test(id , mc , parent,sl) values
(1,     '其他应付' , '-1' , 0),
(101, '社保金' , '1' , 5) ,
(102, '社保金'     , '1' , 5 ),
(10101,'社保金A' , '101' , 12),
(10102,'社保金A' , '101',  13)
go
select * from test 
go
with mx as (
select a.id,a.mc,a.parent ,a.sl 
  from test a 
 where not exists(select * from test b where b.id like a.id + '_%')
)
select t.id , t.mc, t.parent,
(select SUM(sl) from mx where id like  t.id + '%') 
from test t
order by 1
go
drop table test 
go


(5 行受影响)
id         mc         parent     sl
---------- ---------- ---------- -----------
1          其他应付       -1         0
101        社保金        1          5
102        社保金        1          5
10101      社保金A       101        12
10102      社保金A       101        13

(5 行受影响)

id         mc         parent     
---------- ---------- ---------- -----------
1          其他应付       -1         30
101        社保金        1          25
10101      社保金A       101        12
10102      社保金A       101        13
102        社保金        1          5

(5 行受影响)



  相关解决方案