I've got a situation where I want to order a column by ascending or descending based on the value of a second column (in essence, if column A is odd, sort column B ascending, otherwise descending). Example:
A | B |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
2 | 4 |
2 | 5 |
2 | 6 |
3 | 7 |
3 | 8 |
3 | 9 |
would be sorted to
A | B |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
2 | 6 |
2 | 5 |
2 | 4 |
3 | 7 |
3 | 8 |
3 | 9 |
Is this even possible in a single SQL query, or would I have to solve this outside of the database? I've got roughly 60 different A-values, so writing a separate query for each possible A-value isn't a viable option.
I tried using "ORDER BY b if (A%2=1, ASC, DESC)", which (unsurprisingly) didn't work.