SQL2005CLR函数扩展-深入环比计算的详解

2020-07-10 08:11:03易采站长站整理

select ‘ 普陀 ‘ , ‘2007-01’ , ‘1863896’ , ‘161.39’ , ‘11549.02’ union
select ‘ 普陀 ‘ , ‘2007-02’ , ‘1614015’ , ‘119.59’ , ‘13496.24’ union
select ‘ 普陀 ‘ , ‘2007-03’ , ‘1059235.19’ , ‘135.21’ , ‘7834’
 
— 测试语句

declare @key varchar ( 40)
declare @b bit
set @key= newid ()
select @b= dbo. xfn_initKey( @key)
select 区域 , TradeMonth, TradePrice, LastMonthPrice,
cast ( round (( Tradeprice- LastMonthPrice)* 100/ LastMonthPrice, 2) as varchar ( 10))+ ‘%’ as 环比 from (
select *, cast ( dbo. xfn_GetPrevMemberValue( @key, 区域 , Tradeprice) as float ) as LastMonthPrice from @t
) t
select @b= dbo. xfn_disposeKey( @key)
 
— 结果
/*
区域   TradeMonth TradePrice             LastMonthPrice         环比
—- ———- ———————- ———————- ———–
闵行   2007-03    8796.67                NULL                   NULL
闵行   2007-04    9267.19                8796.67                5.35%
闵行   2007-05    9335.26                9267.19                 0.73%
浦东   2007-01    8976.73                NULL                   NULL
浦东   2007-02    12568.05               8976.73                40.01%
浦东   2007-03    8023.98                12568                  -36.16%
浦东   2007-04    18337.23                8023.98                128.53%
相关文章 大家在看