Answer the question
In order to leave comments, you need to log in
Algorithm for sorting Order by by Case in SQL
Table:
first_column second_column
1 11
1 12
2 21
1 13
2 22
3 31
3 32
4 23
select * from TableTemp order by case when first_column = 1 then second_column
else first_column end
first_column second_colum
2 22
2 21
3 31
3 32
4 23
1 11
1 12
1 13
Answer the question
In order to leave comments, you need to log in
The case value for each row is calculated and sorted by that value. For convenience, I showed what case will be equal to in each line, and it will be sorted by this number.
first_column second_colum Case_Value
2 22 2
2 21 2
3 31 3
3 32 3
4 23 4
1 11 11
1 12 12
1 13 13
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question