Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

SQL multiple column ordering

How might I sort by numerous sections in SQL and in various directions? column1 would be arranged to descend and column2 ascending.
by

2 Answers

espadacoder11

ORDER BY column1 DESC, column2

This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two or more rows are equal.
kshitijrana14
You can use multiple ordering on multiple condition,
ORDER BY 
(CASE
WHEN @AlphabetBy = 2 THEN [Drug Name]
END) ASC,
CASE
WHEN @TopBy = 1 THEN [Rx Count]
WHEN @TopBy = 2 THEN [Cost]
WHEN @TopBy = 3 THEN [Revenue]
END DESC

Login / Signup to Answer the Question.