金额表格式:时间,单位ID,项目ID,金额
项目表格式:项目ID,项目名称 (如原料,辅料,水电费)
单位表格式:单位ID,单位名称(如铸造车间,机修车间)现在要进行的是统计出来一个大表显示在某段时间内各单位的金额汇总:
表头各列的名称为各个项目名称,每行开头是各个单位名称,表中数据为该单位在某段时间的某个项目花费金额(三月到五月)
原料 辅料 水电费
铸造车间 9000 2000 3000
机修车间 1000 2000 5000这个视图怎么写呢?有其他的实现方法也行!
项目表格式:项目ID,项目名称 (如原料,辅料,水电费)
单位表格式:单位ID,单位名称(如铸造车间,机修车间)现在要进行的是统计出来一个大表显示在某段时间内各单位的金额汇总:
表头各列的名称为各个项目名称,每行开头是各个单位名称,表中数据为该单位在某段时间的某个项目花费金额(三月到五月)
原料 辅料 水电费
铸造车间 9000 2000 3000
机修车间 1000 2000 5000这个视图怎么写呢?有其他的实现方法也行!
---------------------------------------------------------------------------------------
create table a(StudentId int,StudnetName varchar(10))
insert into a select 1,'张三'
union all select 2,'李四'create table b(Id int,colName varchar(10))
insert into b select 1,'shuXue'
union all select 2,'yuWen'
union all select 3,'yingYu'create table d(StudentId int,colId int,chengJi int)
insert into d select 1 ,1 ,90
union all select 1 ,2 ,90
union all select 1 ,3 ,95
union all select 2 ,1 ,100
union all select 2 ,2 ,90
union all select 2 ,3 ,80
GOcreate view aa
as
select d.StudentId,StudnetName,colName,chengJi
from
a,b,d
where
a.StudentId=d.StudentId and
b.Id=d.colId
GOdeclare @sql varchar(8000)
set @sql='select StudentId,StudnetName'
select @sql=@sql+',['+colName+']=max(case colName when '''+colName+''' then chengJi else 0 end)' from aa group by colName
/*
将此句按程序方式来理解,事实上是一个循环赋值语句:
@sql=@sql+动态取得的值
差别仅在于SELECT不需要加上循环条件
此句需要用SELECT才可以从表中提取,用SET赋值不行
*/
select @sql
exec(@sql+' from aa group by StudentId,StudnetName')
Godrop view aa
drop table a,b,d
Create Table 金额表
(时间 DateTime,
单位ID Int,
项目ID Int,
金额 Int)Create Table 项目表
(项目ID Int,
项目名称 Nvarchar(20))Create Table 单位表
(单位ID Int,
单位名称 Nvarchar(20))
--插入數據
Insert 金额表 Select '2006-03-01',1,1,5000
Union All Select '2006-03-21',1,1,4000
Union All Select '2006-03-21',1,2,2000
Union All Select '2006-03-21',1,3,3000
Union All Select '2006-03-21',2,1,1000
Union All Select '2006-03-21',2,2,2000
Union All Select '2006-03-21',2,3,2000
Union All Select '2006-03-21',2,3,3000Insert 项目表 Select 1,N'原料'
Union All Select 2,N'辅料'
Union All Select 3,N'水电费'Insert 单位表 Select 1,N'铸造车间'
Union All Select 2,N'机修车间'
GO
--建立視圖
Create View V_List
As
Select
A.时间,
B.单位名称,
C.项目名称,
A.金额
From 金额表 A
Left Join 单位表 B
On A.单位ID=B.单位ID
Left Join 项目表 C
On A.项目ID=C.项目ID
GO
--建立存儲過程
--Select *From V_List
Create ProceDure SP_GetTotal
As
Declare @S Nvarchar(4000)
Select @S=''
Select @S=@S+N',SUM(Case 项目名称 When N'''+项目名称+N''' Then 金额 Else 0 End) As ' +项目名称
From 项目表 Order By 项目ID
Select @S=N'Select 单位名称'+@S+N' From V_List Group By 单位名称'
EXEC(@S)
GO
--測試
EXEC SP_GetTotal
GO
--刪除測試環境
Drop Table 金额表,单位表,项目表
Drop View V_List
Drop ProceDure SP_GetTotal
--結果
/*
单位名称 原料 辅料 水电费
机修车间 1000 2000 5000
铸造车间 9000 2000 3000
*/
将此句按程序方式来理解,事实上是一个循环赋值语句:
@sql=@sql+动态取得的值
差别仅在于SELECT不需要加上循环条件
此句需要用SELECT才可以从表中提取,用SET赋值不行
*/是上面的一个注释,我刚开始搞动态SQL时,对这题做的一个注释说明.希望对你有所帮助.重要的是治本,我就不改代码给你了.
As
Declare @S Nvarchar(4000)
If Exists (Select *From SysObjects Where Name='V_Total' And Xtype='V')
Drop View V_Total
Select @S=''
Select @S=@S+N',SUM(Case 项目名称 When N'''+项目名称+N''' Then 金额 Else 0 End) As ' +项目名称
From 项目表 Order By 项目ID
Select @S=N'Create View V_Total As Select 单位名称'+@S+N' From V_List Group By 单位名称'
EXEC(@S)
GO
--測試
EXEC SP_GetTotalSelect *From V_Total
GOV_Total即為你要的視圖