在SQL Server 2008中对数字进行排序时,如何首先显示正数而不是负数。
例: 假设我有数字-4,-3,null,2,3 然后预期结果:2,3,-4,-3,null
答案 0 :(得分:4)
根据您的小例子,您似乎希望正数按递增顺序,然后按负数增加。
这使问题稍微有点儿。试试这个order by
条款:
order by (case when col > 0 then 2 when col < 0 then 1 else 0 end) desc,
col asc
以下是一个例子:
with t as (select 2 as col union all select 3 union all select -4 union all select -3 union all select null)
select *
from t
order by (case when col > 0 then 2 when col < 0 then 1 else 0 end) desc,
col asc;