| View previous topic :: View next topic |
| Author |
Message |
hwoehrle General User

Joined: 21 Sep 2009 Posts: 14 Location: Germany, Ulm
|
Posted: Thu May 20, 2010 1:34 am Post subject: [SOLVED] ORDER BY with conditions |
|
|
Hi,
is there a possibility to order a query by conditions? For example something like::
ORDER BY field1 ASC IF field2<100
ELSE
ORDER BY field1 DESC
Last edited by hwoehrle on Fri May 21, 2010 5:47 am; edited 1 time in total |
|
| Back to top |
|
 |
Sliderule Super User


Joined: 29 May 2004 Posts: 2473 Location: 3rd Rock From The Sun
|
Posted: Thu May 20, 2010 7:10 am Post subject: |
|
|
hwoehrle:
You asked:
| hwoehrle wrote: | | is there a possibility to order a query by conditions? |
Yes.
| Code: |
Order By CASEWHEN("field2" < 100, "field1", 100) Asc,
"field1" Desc |
Explanation: The CASEWHEN is like a spreadsheet IF statement. Therefore, it will first test for the condition of "field2" < 100 . . . if so, return "field1". Otherwise, it will return 100 ( I used 100, since, the condition was 'checking < 100' . . . could have also used any other LARGE number . . . for example . . . 9999999). Since, will have numerous rows with a 'tie' ( same value ) . . . 100 . . . will use the second condition ( in descending order ).
I hope this helps, please be sure to let me / us know.
Sliderule
/Thanks to add [Solved] in your first post title ( edit button ) if your issue has been fixed / resolved. |
|
| Back to top |
|
 |
hwoehrle General User

Joined: 21 Sep 2009 Posts: 14 Location: Germany, Ulm
|
Posted: Fri May 21, 2010 5:47 am Post subject: |
|
|
sliderule you really own the OOo philosopher's stone!
Thanks again, works great |
|
| Back to top |
|
 |
|