Reputation: 29877
I have a table, TableA, that looks like this:
ColA ColB
0 20
1 10
0 5
1 15
I want to sort the rows so that all the records where ColA = 0 are listed first, and then these records are sorted by ColB ascendingly. After all the rows with ColA = 0 are listed, I then want to list all the rows with ColA = 1 but these rows sorted by ColB descendingly.
The results should look like this:
ColA ColB
0 5
0 20
1 15
1 10
Is this possible? Thanks for your help.
Upvotes: 1
Views: 1144
Reputation: 5248
select t.ColA, t.ColB
from t
order by t.ColA
,case when t.ColA = 0 then t.ColB end asc
,case when t.ColA = 1 then t.ColB end desc
Upvotes: 1
Reputation: 6944
SELECT ColA,ColB
FROM table
ORDER BY
ColA, (CASE WHEN ColA = 1 THEN -1 * ColB ELSE ColB END)
Upvotes: 0