D
D
des1roer2015-05-25 08:24:28
PostgreSQL
des1roer, 2015-05-25 08:24:28

Postgres closest number?

There is a table - the ratio.
That is, for 0.5 - 3
For 1.0 - 5.4
The dependence is empirical (ie calculated in real conditions). The formula does not describe.
Table step - 0.5
How, depending on what the user entered, to pull out the corresponding number?
y5bAxLg.png
Smartly, you need to calculate according to the formula

y = ((x - x1) / (x2 - x1)) * (y2 - y1) + y1;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2015-05-25
@des1roer

select *  from (
select t.*, abs(value - 1.1) as diff
from   t
order by diff asc  
limit 2
) x
order by value

find the next two numbers

A
Andrey Mokhov, 2015-05-25
@mokhovcom

sort by minimum difference and take the first value...

SELECT ...
FROM ...
ORDER @ x-1.3 ASC
LIMIT 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question