問題描述
SQL Server 中的 OrderBy 將正值放在負值之前 (OrderBy in SQL Server to put positive values before negative values)
I have table in SQL Server which contains a column of type "int". The column can contain positive as well as negative values. I want to carry out sorting based on this column values such that rows with positive values in this column come before the negative values.
Example:
Code SortColumn
A 1
B 5
C ‑1
D ‑3
E 0
F 2
Desired Output:
Code SortColumn
E 0
A 1
F 2
B 5
C ‑3
D ‑1
‑‑‑‑‑
參考解法
方法 1:
Select * from Table
order by
Case when sortcolumn<0 then 1 else 0 end
,sortcolumn