SQL Server 中的 OrderBy 将正值放在负值之前 [英] OrderBy in SQL Server to put positive values before negative values

查看:32
本文介绍了SQL Server 中的 OrderBy 将正值放在负值之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中有一个表,其中包含一个int"类型的列.该列可以包含正值和负值.我想根据此列值进行排序,使此列中具有正值的行排在负值之前.

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.

示例:

Code SortColumn
A     1
B     5
C    -1
D    -3
E     0
F     2

期望的输出:

Code SortColumn
E        0
A        1
F        2
B        5
C       -3
D       -1

推荐答案

Select * from Table
order by 
Case when sortcolumn<0 then 1 else 0 end
,sortcolumn

这篇关于SQL Server 中的 OrderBy 将正值放在负值之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆