I have an sql table, in which I need to select the data, that is sorted based on a value. For example,
If the sort value is 1, then I need to sort by column1, column2 and then column3.
If the sort value is 2, then I need to sort by column2, column1 and then column3.
If the sort value is 3, then I need to sort by column3, column1 and then column2.
Can anyone tell me how can I achieve this in sql without using if else as below:
IF @SortOrder = 1
THEN
select * from table order by c1,c2,c3
end
IF @SortOrder = 2
THEN
select * from table order by c2,c1,c3
end
IF @SortOrder = 3
THEN
select * from table order by c3,c1,c2
end
What I have tried:
I have tried
select * from table
order by case when @SortOrder = 1 then c1,c2,c3
when @SortOrder = 2 then c2,c1,c3
when @SortOrder = 3 then c3,c1,c2
end