0

I would like to sort database query by:

number value DESCENDING considering String property from another field

Something like:

ORDER BY money DESC,
currency = 'EUR'

Example:

money    |     currency
-----------------------
200      |     EUR
300      |     USD
500      |     USD
100      |     EUR
400      |     EUR

I would like to sort money in descending fashion only when currency is equal to EUR. I don't want other currency to be considered in sorting such as:

money    |     currency
-----------------------
400      |     EUR
200      |     EUR
100      |     EUR
300      |     USD
500      |     USD

I don't need the sorting after EUR. Could be totally random

krzakov
  • 3,871
  • 11
  • 37
  • 52
  • Possible duplicate of [SQL multiple column ordering](https://stackoverflow.com/questions/2051162/sql-multiple-column-ordering) – kiner_shah Jun 25 '18 at 12:00

2 Answers2

2

You can use multiple keys in the order by:

order by (currency = 'EUR') desc,
         money desc

This orders the remaining currencies in descending order. That is a by-product. If you specifically want them to be random:

order by (currency = 'EUR') desc,
         (case when currency = 'EUR' then money end) desc,
         rand()

But that seems like overkill.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

If you really want an order condition only on 'EUR' value you can do something like this :

 ORDER BY (currency = 'EUR') * money DESC
A. Colonna
  • 852
  • 7
  • 10