您的位置:首页 > 数据库

SQL Server 2005 Recursion(递归) and WITH Clause

2010-04-14 10:42 537 查看
代码 1 /*
2 from: http://www.eggheadcafe.com/articles/sql_server_recursion_with_clause.asp 3 http://msdn.microsoft.com/en-us/library/ms186243.aspx 4 http://msdn.microsoft.com/en-us/library/aa175801%28SQL.80%29.aspx 5 SQL Server 2005 Recursion and WITH Clause
6 SQL Server 2005的递归和WITH子句
7 */
8 ---1 Table of Contents Hierarchy
9 set nocount on
10
11 declare @Sample1 table
12 (
13 RecordID int Primary key NOT NULL ,
14 ParentRecordID int,
15 SortOrder int,
16 Description nvarchar(100),
17 Salary money
18 )
19
20 /* Start loading of test data */
21 insert into @Sample1 values(1,null,null,'CEO',10)
22 insert into @Sample1 values(2,1,1,'Vice Pres. Marketing',9)
23 insert into @Sample1 values(3,1,2,'Vice Pres. Ops-',8)
24 insert into @Sample1 values(4,2,1,'Marketing Director - Direct Mail',7)
25 insert into @Sample1 values(5,2,2,'Marketing Director - TV',6)
26 insert into @Sample1 values(6,1,3,'Vice Pres. - Research',5)
27 insert into @Sample1 values(7,4,1,'Human Resources Director',4)
28 insert into @Sample1 values(8,4,2,'Some other item',3)
29 insert into @Sample1 values(9,6,1,'Research Analyst',2)
30
31 set nocount off;
32
33 with RecursionCTE (RecordID,ParentRecordID,SortOrder,Salary,TOC)
34 as
35 (
36 select RecordID,ParentRecordID,SortOrder,Salary,
37 convert(varchar(100),'') TOC
38 from @Sample1
39 where ParentRecordID is null
40 union all
41 select R1.RecordID,
42 R1.ParentRecordID,
43 R1.SortOrder,
44 R1.Salary,
45 case when DataLength(R2.TOC) > 0
46 then convert(varchar(100),R2.TOC + '.'
47 + cast(R1.SortOrder as varchar(10)))
48 else convert(varchar(100),
49 cast(R1.SortOrder as varchar(10)))
50 end as TOC
51 from @Sample1 as R1
52 join RecursionCTE as R2 on R1.ParentRecordID = R2.RecordID
53 )
54
55 select * from RecursionCTE order by ParentRecordID,SortOrder asc
56
57 --2 Sum Up Subordinate Salaries of All Employees
58
59 set nocount on
60
61 declare @Sample1 table
62 (
63 RecordID int Primary key NOT NULL ,
64 ParentRecordID int,
65 SortOrder int,
66 Description nvarchar(100),
67 Salary money
68 )
69
70 /* Start loading of test data */
71 insert into @Sample1 values(1,null,null,'CEO',10)
72 insert into @Sample1 values(2,1,1,'Vice Pres. Marketing',9)
73 insert into @Sample1 values(3,1,2,'Vice Pres. Ops-',8)
74 insert into @Sample1 values(4,2,1,'Marketing Director - Direct Mail',7)
75 insert into @Sample1 values(5,2,2,'Marketing Director - TV',6)
76 insert into @Sample1 values(6,1,3,'Vice Pres. - Research',5)
77 insert into @Sample1 values(7,4,1,'Human Resources Director',4)
78 insert into @Sample1 values(8,4,2,'Some other item',3)
79 insert into @Sample1 values(9,6,1,'Research Analyst',2)
80
81 set nocount off;
82
83 with RecursionCTE (RecordID,ParentRecordID,SortOrder,Salary)
84 as
85 (
86 select RecordID,ParentRecordID,SortOrder,Salary
87 from @Sample1
88 where ParentRecordID is null
89 union all
90 select R1.RecordID,
91 R1.ParentRecordID,
92 R1.SortOrder,
93 R1.Salary
94 from @Sample1 as R1
95 join RecursionCTE as R2 on R1.ParentRecordID = R2.RecordID
96 )
97 select sum(R1.salary) as Salary
98 from @Sample1 as R1
99 JOIN RecursionCTE as R2
on R1.RecordID = R2.RecordID
--3 Sum Up Subordinate Salaries of a Specific Employee
set nocount on

declare @Sample1 table
(
RecordID int Primary key NOT NULL ,
ParentRecordID int,
SortOrder int,
Description nvarchar(100),
Salary money
)

/* Start loading of test data */
insert into @Sample1 values(1,null,null,'CEO',10)
insert into @Sample1 values(2,1,1,'Vice Pres. Marketing',9)
insert into @Sample1 values(3,1,2,'Vice Pres. Ops-',8)
insert into @Sample1 values(4,2,1,'Marketing Director - Direct Mail',7)
insert into @Sample1 values(5,2,2,'Marketing Director - TV',6)
insert into @Sample1 values(6,1,3,'Vice Pres. - Research',5)
insert into @Sample1 values(7,4,1,'Human Resources Director',4)
insert into @Sample1 values(8,4,2,'Some other item',3)
insert into @Sample1 values(9,6,1,'Research Analyst',2)

set nocount off;

with RecursionCTE (RecordID,ParentRecordID,SortOrder,Salary)
as
(
select RecordID,ParentRecordID,SortOrder,Salary
from @Sample1
where ParentRecordID =2 -- specific employee id
union all
select R1.RecordID,
R1.ParentRecordID,
R1.SortOrder,
R1.Salary
from @Sample1 as R1
join RecursionCTE as R2 on R1.ParentRecordID = R2.RecordID
)
select sum(R1.salary) as Salary
from @Sample1 as R1
JOIN RecursionCTE as R2
on R1.RecordID = R2.RecordID

--4 Manager to Subordinate Salary Differential

set nocount on

declare @Sample1 table
(
RecordID int Primary key NOT NULL ,
ParentRecordID int,
SortOrder int,
Description nvarchar(100),
Salary money
)

/* Start loading of test data */
insert into @Sample1 values(1,null,null,'CEO',10)
insert into @Sample1 values(2,1,1,'Vice Pres. Marketing',9)
insert into @Sample1 values(3,1,2,'Vice Pres. Ops-',8)
insert into @Sample1 values(4,2,1,'Marketing Director - Direct Mail',7)
insert into @Sample1 values(5,2,2,'Marketing Director - TV',6)
insert into @Sample1 values(6,1,3,'Vice Pres. - Research',5)
insert into @Sample1 values(7,4,1,'Human Resources Director',4)
insert into @Sample1 values(8,4,2,'Some other item',3)
insert into @Sample1 values(9,6,1,'Research Analyst',2)

set nocount off;

with RecursionCTE (RecordID,ParentRecordID,SortOrder,ParentSalary,Salary,Differential)
as
(
select RecordID,ParentRecordID,SortOrder,
convert(money,null) as ParentSalary,
Salary,
convert(money,null) as Differential
from @Sample1
where ParentRecordID is null
union all
select R1.RecordID,
R1.ParentRecordID,
R1.SortOrder,
convert(money,R2.Salary) as ParentSalary,
R1.Salary,
convert(money,R2.Salary - R1.Salary) as Differential
from @Sample1 as R1
join RecursionCTE as R2 on R1.ParentRecordID = R2.RecordID

)

select * from RecursionCTE order by ParentRecordID,SortOrder asc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐