Skip to content
Advertisement

Find closest or higher values in SQL

I have a table:

table1

I have to generate the following table:

table2

So, table2 should have all values from 1 to 100. There are 3 cases:

  • If it’s an exact match, for ex. rank 25, value would be 120
  • Find closest, for ex. for rank 9 in table2, we do NOT have exact match, but 1 is closest to 9 (9-1 = 8 whereas 25-9 = 16), so assign value of 1
  • If there is equal distribution from both sides, use higher rank value, for ex. for rank 27, we have 25 as well as 29 which are equally distant, so take higher value which is 29 and assign value.

Advertisement

Answer

something like

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