While your logic and above solutions are good for small scale, if you are talking about more than 65000 items, you need a solution which is scalable.
My suggestion is to split this task to 2 steps.
Step 1
Create a temporary table,
This temporary table will have 3 columns minumum
TEMP_ITEM_ORDER_TABLE (
session_key varchar2(50),
item_id number,
item_report_order number
)
Each time user orders such a query, insert data ,i.e item ids and their sequence no into this temporary table with some unique key to identify user session (possibly user id or session id). This trick is to avoid collision of item lists when multiple users simultaneously fire reports.
Step 2
Now fire your report query joining your main table, temp table with session_key
. In the query order data based on your input order (stored in temp table already)
SELECT
T1.* , T2.item_report_order
FROM ITEM T1, TEMP_ITEM_ORDER_TABLE T2
WHERE T1.ITEM_ID = T2.ITEM_ID
AND T2.session_key = :input_session_key
ORDER BY t2.item_report_order
This method is
- database agnostic
- scalable with any number of inputs
- Gives best possible performance
Note: To further improve query performance, create index on session_key, item_id in temp table also create index on item_id on ITEM table (if not exists already)
Edit:
Oracle offers Global Temporary Table feature, which creates has features to allow records only with in session and auto clean up upon commit/end of session etc. You can make use of this feature and avoid session key, but this solution can not be replicated on other database products unless they support similar feature.