M
M
Morfeey2018-11-20 10:45:54
SQL
Morfeey, 2018-11-20 10:45:54

Difference between SQL table values?

There is a table in which there is a field with random values ​​in figures. We need a query in which the 2 closest values ​​​​will be selected, one of which will be different from the other at least by one.
Please help with syntax.
Table "code", field "code_number".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2019-03-14
@LaRN

You can try like this:

select top 1 c1.code_number as c1, c2.code_number as c2, min(c2.code_number - c1.code_number) as delta
  from code as c1
  join code as c2
    on c2.code_number > c1.code_number
 group by c1.code_number, c2.code_number
 having min(c2.code_number - c1.code_number) > 0
 order by 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question