OrderBy in SQL Server to put positive values before negative values

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:49:23

问题


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


来源:https://stackoverflow.com/questions/14908147/orderby-in-sql-server-to-put-positive-values-before-negative-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!