I've got a sql query (using Firebird as the RDBMS) in which I need to order the results by a field, EDITION. I need to order by the contents of the field, however. i.e. "NE" goes first, "OE" goes second, "OP" goes third, and blanks go last. Unfortunately, I don't have a clue how this could be accomplished. All I've ever done is ORDER BY [FIELD] ASC/DESC and nothing else.
Any suggestions?
Edit: I really should clarify: I was just hoping to learn more here. I have it now that I just have multiple select statements defining which to show first. The query is rather large and I was really hoping to learn possibly a more effecient way of doing this: example:
SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='NE' UNION SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='OE' UNION SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='OP' UNION (etc...)
select type , COUNT from TABLE order by FIELD(type,'A','B','C','D') ; It works fine if the column type has value for 'A,B,C,D' . In some cases the order by FIELD('A','B','C','D') some columns may not have value in table . In this cases I want to put 0 for it and construct a result .
By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.
Order By Case Edition When 'NE' Then 1 When 'OE' Then 2 When 'OP' Then 3 Else 4 End
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With