Hi @Sam332 ,
Please refer to:
declare @test table(month int,year int,count int)
insert into @test values(1 ,2019 ,74),(2 ,2019 ,47),(3 ,2019, 2)
,(1, 2020 ,121),(2 ,2020 ,117),(3 ,2020, 90),(7 ,2020, 63),(8 ,2020 ,58),(9, 2020, 46)
;with cte1
as(select *,CAST(count*1.0*100/sum(count) over(partition by year) as decimal(10,2)) per1,
row_number() over(partition by year order by month) rn from @test)
,cte2
as(select c1.month month1,c1.year year1,c1.count count1,c1.per1,c2.per1 per2
from cte1 c1
left join cte1 c2
on c1.rn=c2.rn and c1.year=c2.year+1)
select *,cast(per1-per2 as varchar(15))+'%' diff from cte2
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues October--https://learn.microsoft.com/en-us/answers/questions/142427/users-always-get-connection-timeout-problem-when-u.html