2014-03-19 130 views
1

我寫:排序日期差異SQL

Select datediff(hh,getutcdate(), End_Date) as "Duration" 
from MasterTable 
order by Duration 

要獲得升序小時值的差異。我得到輸出,

 
Duration 
-------- 
    -259 
    -210 
     5 
     10 
     22 
     35 
     75 
    105 
    235 

但我想,排序必須根據正數和負數的小時值來完成。所以,我需要輸出爲:

 
Duration 
-------- 
     5 
     10 
     22 
     35 
     75 
    105 
    235 
    -259 
    -210 

是否有任何調整來做這樣的任務?提前致謝。

+2

不應該序列尾是:...,.., - 210,235,-259 –

回答

1

如果你想要的順序,你已經在問題中指出,它是所有正數的先升序,然後是負數的升序,它應該是:

Select datediff(hh,getutcdate(),End_Date) as "Duration" from MasterTable 
order by CASE WHEN Duration >= 0 THEN 0 ELSE 1 END,Duration 
0

理解,你需要的是忽略時間標誌:

嘗試使用ABS:

Select datediff(hh,getutcdate(),End_Date) as "Duration" from MasterTable order by ABS(Duration) 
0

試試這個,

Select Abs(datediff(hh,getutcdate(),End_Date)) as "Duration" from MasterTable order by Abs(Duration)