Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - retain ordering based on the query params

Tags:

sql

oracle

I'm trying to perform a SELECT with an IN clause and I would like to be able to have the results returned in the same order as the elements in my list for the IN. For example:

SELECT * FROM orders WHERE order_no IN ('B123', 'B483', 'B100', 'B932', ...);

and I would want them to come back in that same order. Ideally, it'd be great if I could have a statement like:

SELECT * FROM orders WHERE order_no IN ('B123', 'B483', 'B100', 'B932', ...)
ORDER BY ('B123', 'B483', 'B100', 'B932', ...);

I've seen examples of queries using the CASE or DECODE keywords to define some sort of custom ordering. But, in all those examples, their ordering was for a predetermined set of options. Whereas, my ordering is completely dependent on what my user enters for their search criteria, so there could be a list of 2 options or a list of 100 to order by...

Any ideas? Some Oracle feature I don't know of, or some way to use CASE or DECODE for a dynamic set?

like image 905
kafuchau Avatar asked May 04 '11 16:05

kafuchau


4 Answers

Insert the values into a temporary table and join your select to that.

You can then do a natural order on your temporary table column.

CREATE GLOBAL TEMPORARY TABLE sort_table (
  value       VARCHAR2(100),
  sort_order  NUMBER
) ON COMMIT DELETE ROWS;

INSERT INTO sort_table VALUES ('B123',1);
INSERT INTO sort_table VALUES ('B483',2);
... etc. ...

select * from mytable
inner join sort_table
on mytable.mycolumn = sort_table.value
order by sort_table.sort_order;

To clear the temporary table, just COMMIT.

like image 75
jenson-button-event Avatar answered Oct 28 '22 10:10

jenson-button-event


I don't know if there is an elegant (or short) solution for this.

If you can build the query dynamically, the following should work:

WITH numbers AS (
   SELECT 1 as sort_order, 'B123' as order_no FROM DUAL
   union all
   SELECT 2 as sort_order, 'B483' as order_no FROM DUAL
   union all
   SELECT 2 as sort_order, 'B100' as order_no FROM DUAL
   union all
   SELECT 2 as sort_order, 'B932' as order_no FROM DUAL
)
SELECT orders.*
FROM numbers  
  LEFT JOIN orders ON orders.ord_no = numbers.ord_no
ORDER BY numbers.sort_order
like image 25
a_horse_with_no_name Avatar answered Oct 28 '22 08:10

a_horse_with_no_name


you can try it will work fine. Check below sql:-

SELECT * FROM orders WHERE order_no IN ('B123', 'B483', 'B100', 'B932') 
ORDER BY DECODE(order_no,'B123','1','B483','2','B100','3','B932','4');
like image 26
user3278731 Avatar answered Oct 28 '22 08:10

user3278731


You can concatenate your variables and order by instr on it like below. I e cannot vouch for the efficiency of this - but must be Okey.Your front end will obviously have to do a bit more work.But constructing a query like this can be open to sql Injection.

SELECT * FROM orders WHERE order_no IN ('B123', 'B483', 'B100', 'B932', ...)
ORDER BY 
  instr ('@B123@'|| '@B483@'||'@B100@'||'@B932@'||... ,'@'|| order_no||'@')
like image 39
josephj1989 Avatar answered Oct 28 '22 08:10

josephj1989