2013-07-24 24 views
0

如何,我需要先顯示正數,負數,而在SQL Server排序號2008的正負號排序,以顯示第一

防爆正數: 可以說我有號碼 - 4,-3,null,2,3 Then then result:2,3,-4,-3,null

+2

http://stackoverflow.com/questions/14908147/orderby-in-sql-server-to-put-positive-values -Before - 負 - 值 – Riv

回答

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;