65

I need to order transaction based on the currency. However I need to implement a custom order by, which makes the USD to always comes on the top, and the rest should be ordered asc.

for example :

  • BHT
  • USD
  • MYR
  • JYP

should be sorted like :

  • USD
  • BHT
  • JPY
  • MYR

Is there a simple way to handle this?

0

8 Answers 8

135

Don't know if this qualifies as simple:

order by 
    case 
       when currency = 'USD' then 1 
       when currency = 'BHT' then 2
       when currency = 'JPY' then 3
       when currency = 'MYR' then 4
       else 5
    end

or a bit more compact but Oracle specific:

order by decode(currency, 'USD', 1, 'BHT', 2, 'JPY', 3, 'MYR', 4, 5)

The above solution using numbers to defined the sort order will not automatically sort currencies correctly that aren't mentioned in the case/decode expression.

To simply put USD at the front and don't care about the rest, the "generated" order criteria must be a character value as well. You can use the following in that case:

order by 
    case 
       when currency = 'USD' then '001' 
       else currency
    end

Which uses an "alphabetical" ordering. This works because characters are sorted after the number digits. (Using 'AAA' instead of '001' would work as well).

5
  • 3
    OP took 4 countries for example purpose. I don't think above is the right way... Its like doing it manually... What If I add country as ARG? Will you change your query again? Nov 12, 2012 at 7:16
  • 1
    @FahimParkar: that's why I added the solution using character ordering. That will put USD at the top and everything else alphabetically (I removed the hardcoded currencies in the second solution).
    – user330315
    Nov 12, 2012 at 7:19
  • hmm, second one is nice solution. +1 for that :) Nov 12, 2012 at 7:22
  • This is nice, thanks! Is there a way i can also filter in my where clause via some kind of new ordering? Like where this field is < that field via my numbering? Jul 31, 2014 at 18:25
  • Is decode slower than that of the case? Over 1 million records?
    – Zeus
    Jan 27, 2015 at 17:43
24

To make sure Your sort is "flexible" and will work with all currencies do this:

SELECT <columns>
FROM <tableName>
ORDER BY DECODE(currencyColumn,'USD', 1, 2), currencyColumn
0
10

A more detailed way of doing this, if you are interesting in sorting certain values to the beginning or end, but have those sorted in their group:

order by
  case when currency in ('USD', 'CAD') 
    then '000'||currency
  when currency in ('ZWD', 'HTG')
    then 'ZZZ'||currency
  else currency 
  end

This will put the USD and CAD at the top of the list (sorted), ZWD and HTG at the bottom, and the rest sorted between those.

1
  • 1
    Great Answer! Helped me avoid doing a "Union". Thanks buddy.
    – Roy Doron
    May 30, 2016 at 12:17
1

Maybe this will help you:

order by decode(currency, 'USD', 1, 2)

or using case

  order by 
      case 
        when currency = 'USD' then 1 
        else 2
      end
0

One more variant with regexp like function FIELD() in MySQL:

select
meas_code,
to_number(regexp_replace(meas_code, replace('(meas1,meas2,meas3)', ',', '|'), instr(replace('(meas1,meas2,meas3)', ',', '|'), meas_code))) ordr
from (select cast(column_value as varchar2(10)) as meas_code from xmltable('''meas1'',''meas2'',''meas3'''))
order by 2
0

If you want a custom order i suggest you to use 'FIELD'.

SELECT * FROM table ORDER BY FIELD(column,"case 1","case 2",...);
-1

You could do the following:

SELECT 
  * 
FROM
  yourtable
ORDER BY
  REPLACE(FIND_IN_SET(currency,'USD,BHT,JPY,MYR'),0,'Z')
-1

I needed to do the same, but with multiple columns, and found Grzegorz W's answer the best for this, with the following simple addition:

SELECT <columns>
FROM <tableName>
ORDER BY DECODE(currencyColumn,'USD', 1, 2), currencyColumn, anotherColumn;
2
  • 1
    Whoever down voted this want to explain why? If my code doesn't match my text, then sure, down vote and explain why you think it is wrong. If not, then really?! Sure, my answer doesn't exactly match the OP's question, but I came across this thread when searching the answer to my problem, and Grzegorz W's answer best matched my problem. So I reposted my exact solution for the next person's benefit who is looking for the same answer as me. This, in my view, is the wonderful thing about stackoverflow. I have found many solutions to my problems from posts just like mine. Aug 22, 2016 at 2:51
  • 1
    Probably downvoted (not by me) because you've added anotherColumn but have not explained what it is or why you'd need to do this. If I posted the exact same answer as you and included yetAnotherColumn I'd expect a downvote. Feb 9, 2017 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.