how to order rows in pl/sql?
i have a table of numbers, i want to order in ascending order if id of table is odd and descending if id is even

Recommended Answers

All 5 Replies

Hi,
You can do that with 2 selects:
For odd ids:
SELECT * FROM table WHERE MOD(ID,2) <> 0 ORDER BY column ASC
For even ids:
SELECT * FROM table WHERE MOD(ID,2) = 0 ORDER BY column DESC

Following query will show first even ids in desc and later it will show odd ids in asc

SELECT * FROM tablename  ORDER BY case when MOD(idcolumnname,2)=0 then -1 else 1 end * idcolumnname asc

"if id of table"? What "id of table"?
You make no sense.
The table has no id. If you select out of it and pass a record identifier you should only get 1 row so there's no need to sort anything.

Or are you maybe refering to an id of another table with which the one you intent to sort a selection from has a foreign key relationship?

i have table1
id col1 col2 col3 col4 col5
1   5    7   4     6    9
2   6    5   9     4    10
3   9    6   8     7    6
4   10   5   8     7    10

i want table to look like

id col1 col2 col3 col4 col5
1   4    5   6     7    9
2   10   9   6     5    4
3   6    6   7     8    9
4   10   10   8    7    5
if id is odd order by asc order 
if id is even order by desc order

Select each record, col1...col5 in a VARRAY(5) OF INTEGER, sort the VARRAY elements using buble-sort, depending on the id column from table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.