Lakmal Premaratne
Lakmal Premaratne

Reputation: 1229

fetch records from table ordered by column value

I have a table with a column to maintain the state of the record. i.e.

-----------------------------
| id  | desc      | state    |
-----------------------------
| 1   | desc 1    | Complete |
| 2   | desc 2    | Open     |
| ... | ...       | ...      |
-----------------------------

I want fetch the records in the order of 'Open' followed by 'Complete'. Can I get this done using one SQL query? If so, how should I write it?

Upvotes: 0

Views: 51

Answers (2)

kiks73
kiks73

Reputation: 3778

Yes, you could do this with the ORDER BY statement and FIELD function:

SELECT * FROM table1 ORDER BY FIELD(state, 'Open', 'Complete')

Upvotes: 2

OracleDev
OracleDev

Reputation: 535

Try something like this:

select * 
from table_name 
order by decode (state, 'Open', 1, 'Complete', 2, 3)

Upvotes: 0

Related Questions