sqlserver 2005 递归

闲话少说

题目:

 

1. 有集团组织结构表Org和部门每月费用表BudgetOrg表包括ID INT 自增型),OrgID (INT 组织编号) , OrgNameVARCHAR(50) 组织名称),ParentID (INT 所属父级OrgID),其中 ParentID -1表示顶级结构,如蓝腾集团。部门每月成本表Budget包括IDINT自增) OrgID INT 组织编号) CostINT 费用) CostName NVARCHAR50)费用科目)AddDate Datetime 费用时间)

组织结构表Org

ID

OrgID

OrgName

ParentID

1

1

蓝腾集团

-1

2

2

浙江新万蓝科技有限公司

1

3

3

人事部

2

4

4

网站部

2

5

5

财务部

2

6

6

浙江兰腾投资有限公司

1

7

7

策划部

6

8

8

调研部

6

部门每月成本表Budget

ID

OrgID

Cost

CostName

AddDate

1

3

1000

餐补

2010-1-20

2

3

2000

电话费

2010-1-21

3

3

3000

打的费

2010-1-23

4

4

5000

餐补

2010-1-11

5

4

12000

电话费

2010-1-12

6

4

3000

打的费

2010-1-15

7

5

600

餐补

2010-1-10

8

5

300

电话费

2010-1-12

9

5

200

打的费

2010-1-25

10

7

1000

餐补

2010-1-01

11

7

900

电话费

2010-1-02

12

7

4090

打的费

2010-1-27

13

7

5000

培训费

2010-1-21

14

7

12000

出差费

2010-1-12

15

8

3000

打的费

2010-1-15

16

8

600

餐补

2010-1-10

17

8

300

电话费

2010-1-12

18

8

22200

科研费

2010-1-23

19

3

2000

餐补

2010-2-20

20

3

3000

电话费

2010-2-21

21

3

4000

打的费

2010-2-23

22

4

5000

餐补

2010-2-11

23

4

11000

电话费

2010-2-12

24

4

2500

打的费

2010-2-15

25

5

1600

餐补

2010-2-10

26

5

3100

电话费

2010-2-12

27

5

2200

打的费

2010-2-25

28

7

12000

餐补

2010-2-01

29

7

100

电话费

2010-2-02

30

7

4000

打的费

2010-2-27

31

7

3000

培训费

2010-2-21

32

7

6000

出差费

2010-2-12

33

8

2300

打的费

2010-2-15

34

8

1000

餐补

2010-2-10

35

8

1300

电话费

2010-2-12

36

8

12200

科研费

2010-2-23

 

请写出统计每个公司费用的SQL语句。

 

sql:

 


CREATE TABLE [Org] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [OrgID] [int] NULL , [OrgName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [ParentID] [int] NULL , CONSTRAINT [PK_Org] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] ) ON [PRIMARY] GO INSERT [Org]( [OrgID], [OrgName], [ParentID]) select 1,N'蓝腾集团',-1 UNION ALL SELECT 2,N'浙江新万蓝科技有限公司',1 UNION ALL SELECT 3,N'人事部',2 UNION ALL SELECT 4,N'网站部',2 UNION ALL SELECT 5,N'财务部',2 UNION ALL SELECT 6,N'浙江兰腾投资有限公司',1 UNION ALL SELECT 7,N'策划部',6 UNION ALL SELECT 8,N'调研部',6 create TABLE [Budget] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [OrgID] [int] NULL , [Cost] [int] NULL , [CostName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [AddDate] [datetime] NULL , CONSTRAINT [PK_Budget] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] ) ON [PRIMARY] GO INSERT INTO [Budget]( [OrgID],Cost, [CostName], [AddDate]) select 3,1000,'餐补',2010-1-20 UNION ALL SELECT 3,2000,'电话费',2010-1-21 UNION ALL SELECT 3,3000,'打的费',2010-1-23 UNION ALL SELECT 4,5000,'餐补',2010-1-11 UNION ALL SELECT 4,12000,'电话费',2010-1-12 UNION ALL SELECT 4,3000,'打的费',2010-1-15 UNION ALL SELECT 5,600,'餐补',2010-1-10 UNION ALL SELECT 5,300,'电话费',2010-1-12 UNION ALL SELECT 5,200,'打的费',2010-1-25 UNION ALL SELECT 7,1000,'餐补',2010-1-01 UNION ALL SELECT 7,900,'电话费',2010-1-02 UNION ALL SELECT 7,4090,'打的费',2010-1-27 UNION ALL SELECT 7,5000,'培训费',2010-1-21 UNION ALL SELECT 7,12000,'出差费',2010-1-12 UNION ALL SELECT 8,3000,'打的费',2010-1-15 UNION ALL SELECT 8,600,'餐补',2010-1-10 UNION ALL SELECT 8,300,'电话费',2010-1-12 UNION ALL SELECT 8,22200,'科研费',2010-1-23 UNION ALL SELECT 3,2000,'餐补',2010-2-20 UNION ALL SELECT 3,3000,'电话费',2010-2-21 UNION ALL SELECT 3,4000,'打的费',2010-2-23 UNION ALL SELECT 4,5000,'餐补',2010-2-11 UNION ALL SELECT 4,11000,'电话费',2010-2-12 UNION ALL SELECT 4,2500,'打的费',2010-2-15 UNION ALL SELECT 5,1600,'餐补',2010-2-10 UNION ALL SELECT 5,3100,'电话费',2010-2-12 UNION ALL SELECT 5,2200,'打的费',2010-2-25 UNION ALL SELECT 7,12000,'餐补',2010-2-01 UNION ALL SELECT 7,100,'电话费',2010-2-02 UNION ALL SELECT 7,4000,'打的费',2010-2-27 UNION ALL SELECT 7,3000,'培训费',2010-2-21 UNION ALL SELECT 7,6000,'出差费',2010-2-12 UNION ALL SELECT 8,2300,'打的费',2010-2-15 UNION ALL SELECT 8,1000,'餐补',2010-2-10 UNION ALL SELECT 8,1300,'电话费',2010-2-12 UNION ALL SELECT 8,12200,'科研费',2010-2-23 go --创建OrgID,ParentID,OrgID总消费的视图 CREATE VIEW [dbo].[ViewSumOrgID] AS SELECT ISNULL(SUM(dbo.Budget.Cost), 0) AS csum, dbo.Org.OrgID, dbo.Org.ParentID FROM dbo.Budget RIGHT OUTER JOIN dbo.Org ON dbo.Budget.OrgID = dbo.Org.OrgID GROUP BY dbo.Org.OrgID, dbo.Org.ParentID GO --创建获取子节点ID的方法 create FUNCTION f_levelid() RETURNS TABLE AS RETURN( WITH stb([id],[levelID]) AS( SELECT OrgID,1 FROM org WHERE ParentID=-1 UNION ALL SELECT A.OrgID,B.[levelID]+1 FROM org A,stb B WHERE A.ParentID=B.[id]) select id FROM stb where stb.[levelID] in (select top 1 [levelID] from stb order by [levelID] desc ) ) GO WITH stb([id],ssum,sParentID) AS( SELECT OrgID,csum,ParentID FROM [ViewSumOrgID] WHERE OrgID in (select * from f_levelid()) UNION ALL SELECT A.OrgID,B.ssum+A.csum,A.ParentID FROM [ViewSumOrgID] A,stb B WHERE A.OrgID=B.sParentID) select * from ( select SUM(ssum) tsum,id from stb group by stb.id) t ,Org where t.id = Org.OrgID

 

结果:

 

 

 

 

Xiaoyuan245437   大大说:
你所说的结果当中可以也包括 人事部呢 ? 人事部 网站 财务 应该都属于 万蓝科技对把? 你要查询的不是每个月的工资消费?

 

 

你所说的结果当中可以也包括 人事部呢 ? 人事部 网站 财务 应该都属于 万蓝科技对把?

查询结果在图片上了,您说的是对的

为了符合原来每个月工资消费的需求我特意改了下

sql如下

 

--修改OrgID,ParentID,OrgID总消费的视图 alter VIEW [dbo].[ViewSumOrgID] AS SELECT ISNULL(SUM(dbo.Budget.Cost), 0) AS csum, dbo.Org.OrgID, dbo.Org.ParentID,MONTH(dbo.Budget.AddDate) as cM FROM dbo.Budget RIGHT OUTER JOIN dbo.Org ON dbo.Budget.OrgID = dbo.Org.OrgID GROUP BY dbo.Org.OrgID, dbo.Org.ParentID,MONTH(dbo.Budget.AddDate) GO WITH stb([id],ssum,sParentID,sM) AS( SELECT OrgID,csum,ParentID,cM FROM [ViewSumOrgID] WHERE OrgID in (select * from f_levelid()) UNION ALL SELECT A.OrgID,B.ssum+A.csum,A.ParentID,B.sM FROM [ViewSumOrgID] A,stb B WHERE A.OrgID=B.sParentID ) select * from ( select SUM(ssum) tsum,id,sM as 月份 from stb group by stb.id,sM) t ,Org where t.id = Org.OrgID

 

 

 

 

 

 

 

 

再次感谢Xiaoyuan245437大大

 

灵感来源 :邹建老大的案例 http://blog.csdn.net/zjcxc/archive/2005/07/27/436328.aspx

 

你可能感兴趣的:(sql,sqlserver,2010,电话,null,join,sql)