I have a table in PostgreSQL with one number column and I have a given number x.
If x is in the table, I want all numbers >= x.
If x is not in the table, I want all numbers > x and the largest number < x.
Example:
id
5
10
15
20
For x = 15 it should return 15 and 20.
For x = 12 it should return 10, 15 and 20.
I have tried the following:
SELECT id FROM table_name WHERE id > 12
UNION
SELECT MAX(id) FROM table_name WHERE id <= 12
which works correctly.
Is there any single-query way? Thank you.
(This is just an example with single column and numbers. The reality is a larger table and datetime column, but the principle should be the same.)
Converted from my comment: