工程编码  投资年 投资月  月投资金额
1         2006    01       1000
1         2006    02       2000
1         2006    03       3000
1         2007    01       4000
2         2006    01       1000
2         2006    02       2000
2         2006    03       3000
2         2007    02       4000创建完视图为
工程编码  投资年  投资月 月投资金额  总计
1         2006    01       1000      1000
1         2006    02       2000      3000
1         2006    03       3000      6000
1         2007    01       4000     10000
2         2006    01       1000      1000
2         2006    02       2000      3000
2         2006    03       3000      6000
2         2007    02       4000     10000
原来是这么写的应该改成什么样
Create View List
As
Select 
A.工程编码,
A.投资年,
A.投资月,
A.月投资金额,
(Select SUM(月投资金额) From TableName Where 工程编码=A.工程编码 And 投资年=A.投资年 And 投资月<=A.投资月) As 总计
From TableName A
GO 

解决方案 »

  1.   

    create table test(工程编码 int,投资年 int,投资月 varchar(10),月投资金额 int)
    insert test select 1,2006,'01',1000
    union all select 1,2006,'02',2000
    union all select 1,2006,'03',3000
    union all select 1,2007,'01',4000
    union all select 2,2006,'01',1000
    union all select 2,2006,'02',2000
    union all select 2,2006,'03',3000
    union all select 2,2007,'02',4000
    select id=identity(int,1,1),* into #t from test
    select *,(select sum(月投资金额) from #t where 工程编码=t.工程编码 and id<=t.id) from #t t