I have the following code where I am selecting the LossAmount value from a table where I create a sequence column along with my LossAmount column, and picking the LossAmount value depending on a specific sequence number.
declare @percent float
set @percent = 0.95
select LossAmount
from (select LossAmount, seq = ROW_NUMBER() over (order by LossAmount desc) from dbo.TestXB_TotalLoss) as p
where p.seq =((select COUNT(*) from dbo.TestXB_TotalLoss)-((select COUNT(*) from dbo.TestXB_TotalLoss)*@percent))
So say my LossAmount column has 1000 rows, the p.seq equation is 1000-1000(0.95). I am picking the LossAmount value at that sequence number (which is 50).
In my case, my parameter is a scalar.
How can I update my code if I have a variety of percents I want to calculate? I have the following table, dbo.Percentiles
Percentiles
0.4
0.6
0.996
For my output, I would like something like this.
Percentiles LossAmount
0.4 ...some #...
0.6 ...some #...
0.996 ...some #...
Any help will be appreciated.
I think this should do you, there are cleaner ways to write it, but I wanted to change your code as little as possible…