Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order by clause 1

Status
Not open for further replies.

sonname

Programmer
May 18, 2001
115
US
I have a table and I want to use a select query to sort the results in ascending order except for one value "other" that I want to always display as the last entry and the rest in alphabetical order. So for example.


Apple
banana
tomoatoes
other


As you can see "O" would come before "T", but this is the exception value that I want to keep at the end of the result set.

Thanks in advance.
 
select f1, f2, f3,
case
when f1 = 'other' then 'zzz'
else f1 end as sortorder
from tb1
order by 4, 1
 
This sorts out correctly but the problem now is that I see 'zzz' as the result instead of 'other'
 
The only thing I can think to do is using a case statement as clapag22 demonstrated and concatenate a value or character to 'other' that equates after all string characters.

maybe something like

Code:
select f1, f2, f3,
case
when f1 = 'other' then '-Other'
else f1 end as sortorder
from tb1
order by 4, 1


"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
Similar to other posts...but using the sortorder field to store either the dummy value or the field to be sorted
Code:
select f1,f2,f3,
sortorder = case rtrim(f1) when 'Other' then 'zzz' else f1 end
from tb1
order by sortorder


Sweep
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top