Say I have the following data
Name Value
===============
Small 10
Medium 100
Large 1000
Imagine that these represent the volumes of boxes. I have some items that I want to put in the boxes, and I want the smallest box possible. I need an SQL query that will:
- Return the row with the smallest row greater than my query parameter
- If there is no such row, then return the largest row.
It is easy to split this up in to two queries (i.e. query point 1 first and if no rows are returned, select the largest number from the table). However, I like to do things in one query if possible to eliminate overhead (both code and context switching), and it looks like it should be possible to do. It's probably very obvious, but the Sun has been shining on me all day and I can't think!
So for example, I want the query to return 10 if you use a parameter of 5, 100 if you use a parameter of 15 and 1000 if you use anything greater than 100 (including numbers greater than 1000).
I'm on Oracle 11g, so any special Oracle goodness is OK.