2011-04-01 62 views
-1

我有一個像這樣在我的表中的數據:使本月的累積和使用SQL Server 2005

> HourWork MonthName 
> 
> 366.00 May 2010  
> 1000.00 Sep 2010  
> 1704.00 Oct 2010  
> 3000.00 Nov 2010 

現在,我想打一個存儲過程來得到這樣的結果:

> HourWork MonthName 
> 
> 366.00 May 2010  
> 1366.00 Sep 2010  
> 2704.00 Oct 2010  
> 4704.00 Nov 2010 

表示獲得下個月的價值,以及一些與當前月份數據。

任何一個給我的方式我怎麼才能得到這樣的結果使用存儲過程。

回答

1
CREATE TABLE WorkTimeLog 
(
    WTLID  int identity NOT NULL, 
    HourWork numeric(7,2) NOT NULL, 
    MonthStart datetime  NOT NULL, 
    PRIMARY KEY(WTLID) 
) 
go 

set dateformat dmy; 

insert WorkTimeLog values (366.00, '01-May-2010') 
insert WorkTimeLog values (1000.00, '01-Sep-2010') 
insert WorkTimeLog values (1704.00, '01-Oct-2010') 
insert WorkTimeLog values (3000.00, '01-Nov-2010'); 
go 

select IsNull((select HourWork from WorkTimeLog where MonthStart = wtl.MonthStart) + (select HourWork from WorkTimeLog where MonthStart = (select MAX(MonthStart) from WorkTimeLog where MonthStart < wtl.MonthStart)), wtl.HourWork) as PairTotal, 
     RIGHT(CONVERT(char(11), wtl.MonthStart, 13), 8) as Period 
from WorkTimeLog as wtl 
order by wtl.MonthStart 
go 

PairTotal Period 
366.00  May 2010 
1366.00 Sep 2010 
2704.00 Oct 2010 
4704.00 Nov 2010