22,207
社区成员
发帖
与我相关
我的任务
分享
declare @Data table (
C CHAR(1) ,
Y INT ,
S1 INT,
S2 INT,
S3 INT,
S4 INT)
INSERT INTO @Data
select 'a',2013,10000,12000,14000,16000 union all
select 'b',2013,2000,2400,2800,3000 union all
select 'a',2014,12000,14000,16000,18000 union all
select 'b',2014,2400,2800,3000,3200 union all
select 'b',2015,2800,3000,3200,3600
;With T as (
select row_number() over (partition by c order by Y asc) as RowNO,*
from @Data
)
select a.C,a.Y, (b.S1-a.S1)*100.0/a.S1 as RS1,(b.S2-a.S2)*100.0/a.S2 as RS2,
(b.S3-a.S3)*100.0/a.S3 as RS3,(b.S4-a.S4)*100.0/a.S4 as RS4
from T a left join T b on a.c = b.c and a.RowNO = b.rowno + 1