Skip to content
Advertisement

How to get interpolation value in SQL Server?

I want to get interpolation value for NULL. Interpolation is a statistical method by which related known values are used to estimate an unknown price or potential yield of a security. Interpolation is achieved by using other established values that are located in sequence with the unknown value.

Here is my sample table and code.

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=673fcd5bc250bd272e8b6da3d0eddb90

I want to get this result:

I use this code. I think this code incorrect.

coeff is the NULL is in order set.

This code is for implementing interpolation.

I tried to find out between the empty values and divide them by the number of spaces.

But, this code is incorrect.

Please, let me know what a good way to fill NULL using linear relationships.

If there is another good way, please recommend it.

Advertisement

Answer

If I assume that you mean linear interpolation between the previous price and the next price based on the number of days that passed, then you can use the following method:

  • Use window functions to get the next and previous days with prices for each row.
  • Use window functions or joins to get the prices on those days as well.
  • Use arithmetic to calculate the linear interpolation.

You SQL Fiddle uses SQL Server, so I assume that is the database you are using. The code looks like this:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement