本文发表在 rolia.net 枫下论坛/* method 1 : use temporary table*/
declare @YTD int
declare @m int /* shoud be a parameter */
/* set parameter value for test */
set @m=3
/* Create a temorary table */
create #monthdays_tbl (
mon int,
days int
)
/* get month days from your table and inserted into the temporary table */
select 1 as mon, m1 as days insert #monthdays_tbl from <your table>
select 2 as mon, m2 as days insert #monthdays_tbl from <your table>
select 3 as mon, m3 as days insert #monthdays_tbl from <your table>
select 4 as mon, m4 as days insert #monthdays_tbl from <your table>
select 5 as mon, m5 as days insert #monthdays_tbl from <your table>
select 6 as mon, m6 as days insert #monthdays_tbl from <your table>
select 7 as mon, m7 as days insert #monthdays_tbl from <your table>
select 8 as mon, m8 as days insert #monthdays_tbl from <your table>
select 9 as mon, m9 as days insert #monthdays_tbl from <your table>
select 10 as mon, m10 as days insert #monthdays_tbl from <your table>
select 11 as mon, m11 as days insert #monthdays_tbl from <your table>
select 12 as mon, m12 as days insert #monthdays_tbl from <your table>
select @YTD = select sum (days) from #monthdays_tbl where mon<=@m
Print 'YTD for month ' + cast (@m to varchar(20)) + ' = ' + cast (@YTD as varchar (20))
/* method 2 - use case statement */
Select @YTD = Case @m
When 1 Then Select m1 From <your table>
When 2 Then Select m1 + m2 From <your table>
When 3 Then Select m1 + m2 + m3 From <your table>
When 4 Then Select m1 + m2 + m3 + m4 From <your table>
When 5 Then Select m1 + m2 + m3 + m4 + m5 From <your table>
When 6 Then Select m1 + m2 + m3 + m4 + m5 + m6 From <your table>
When 7 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 From <your table>
When 8 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 From <your table>
When 9 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 From <your table>
When 10 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 From <your table>
When 11 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 From <your table>
When 12 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + m12 From <your table>
End
Print 'YTD for month ' + cast (@m to varchar(20)) + ' = ' + cast (@YTD as varchar (20))更多精彩文章及讨论,请光临枫下论坛 rolia.net
declare @YTD int
declare @m int /* shoud be a parameter */
/* set parameter value for test */
set @m=3
/* Create a temorary table */
create #monthdays_tbl (
mon int,
days int
)
/* get month days from your table and inserted into the temporary table */
select 1 as mon, m1 as days insert #monthdays_tbl from <your table>
select 2 as mon, m2 as days insert #monthdays_tbl from <your table>
select 3 as mon, m3 as days insert #monthdays_tbl from <your table>
select 4 as mon, m4 as days insert #monthdays_tbl from <your table>
select 5 as mon, m5 as days insert #monthdays_tbl from <your table>
select 6 as mon, m6 as days insert #monthdays_tbl from <your table>
select 7 as mon, m7 as days insert #monthdays_tbl from <your table>
select 8 as mon, m8 as days insert #monthdays_tbl from <your table>
select 9 as mon, m9 as days insert #monthdays_tbl from <your table>
select 10 as mon, m10 as days insert #monthdays_tbl from <your table>
select 11 as mon, m11 as days insert #monthdays_tbl from <your table>
select 12 as mon, m12 as days insert #monthdays_tbl from <your table>
select @YTD = select sum (days) from #monthdays_tbl where mon<=@m
Print 'YTD for month ' + cast (@m to varchar(20)) + ' = ' + cast (@YTD as varchar (20))
/* method 2 - use case statement */
Select @YTD = Case @m
When 1 Then Select m1 From <your table>
When 2 Then Select m1 + m2 From <your table>
When 3 Then Select m1 + m2 + m3 From <your table>
When 4 Then Select m1 + m2 + m3 + m4 From <your table>
When 5 Then Select m1 + m2 + m3 + m4 + m5 From <your table>
When 6 Then Select m1 + m2 + m3 + m4 + m5 + m6 From <your table>
When 7 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 From <your table>
When 8 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 From <your table>
When 9 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 From <your table>
When 10 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 From <your table>
When 11 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 From <your table>
When 12 Then Select m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + m12 From <your table>
End
Print 'YTD for month ' + cast (@m to varchar(20)) + ' = ' + cast (@YTD as varchar (20))更多精彩文章及讨论,请光临枫下论坛 rolia.net