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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ill test it and get back to you with answer
works as a charm. I need to translate it to queryDSL somehow, seems to be tricky
1

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

 ORDER BY (currency = 'EUR') * money DESC

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.