/*
ID NUM
1 5
2 3
3 2
4 5
5 4
6 5
7 3
8 2
9 4
10 3
*/
--sql2005的一种解法:
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[NUM] int)
insert [tb]
select 1,5 union all
select 2,3 union all
select 3,2 union all
select 4,5 union all
select 5,4 union all
select 6,5 union all
select 7,3 union all
select 8,2 union all
select 9,4 union all
select 10,3
go
--select * from [tb]
with szx as
(
select *,path=cast(id as varchar(8000)),total=num from tb
union all
select b.id,b.num,a.path+'-'+rtrim(b.id),a.total+b.num
from szx a join tb b on a.id0
--1.
/*
2 3
3 2
4 5
6 5
*/
--2.
/*
2 3
3 2
4 5
8 2
10 3
*/
--3....
9531

被折叠的 条评论
为什么被折叠?



