2014-05-07 81 views
0

從我的角度來看,我得到一個值爲'0 0:0:0.343009'的列,它顯示了兩個時間戳之間的差異,以毫秒爲單位的精度。將TIMESTAMP差異列數據轉換爲字符串格式

我想將它們顯示爲像343毫秒或其他值的字符串,但應該是毫秒轉換。

我有similar post in the past但該時間列是DATE數據類型,這次是TIMESTAMP。我正在使用FLOOR函數將輸出更改爲數值以顯示更加用戶友好的結果。

我使用示例查詢來查找日期的差異。這裏created_timeTIMESTAMP數據類型:

select msg_guid, 
    (max(case when payload_type = 1 then created_time end) - 
    (case when max(case when payload_type = 2 then created_time end) <> 
       trunc(max(case when payload_type = 2 then created_time end)) 
      then max(case when payload_type = 2 then created_time end) 
      when max(case when payload_type = 3 then created_time end) <> 
       trunc(max(case when payload_type = 3 then created_time end)) 
      then max(case when payload_type = 3 then created_time end) 
    end) 
    ) as diff    
from table t 
group by msg_guid; 
+0

列的值有兩個日期的差異,使用http://stackoverflow.com/questions/23426364/write-oracle-db-view-based-on-different-column-time-stamp-區別 – kumarb

回答

2

當您添加或減去時間戳,the result is an interval,不是另一個時間戳。您可以使用the extract function將組件拉出。如果你的價值始終會是一秒,你可以只提取秒,由一千乘以獲得毫秒:

with t as (
    select 1 as msg_guid, 
    interval '0 0:0:0.343009' day to second as diff 
    from dual 
) 
select trunc(extract (second from diff) * 1000) 
from t; 

TRUNC(EXTRACT(SECONDFROMDIFF)*1000) 
----------------------------------- 
           343 

這裏你真正的查詢會採取假CTE的我所使用的地方間隔文字。

如果間隔可能會超過一秒鐘,那麼你大概會想以毫秒爲單位的整數值,所以你需要解壓的所有元素,並把它們相加,乘以每個基礎上,他們代表了什麼 - 所以一整天的時間是86400000毫秒等等。純元素會出來這樣的:

column diff format a25 
with t as (
    select 1 as msg_guid, 
    systimestamp - trunc(systimestamp) as diff 
    from dual 
) 
select diff, 
    extract (day from diff) as dd, 
    extract (hour from diff) as hh, 
    extract (minute from diff) as mi, 
    extract (second from diff) as ss 
from t; 

DIFF       DD   HH   MI   SS 
---------------------- ---------- ---------- ---------- ---------- 
0 9:13:26.150627    0   9   13 26.150627 

而且你將它們組合,如:根據您剛纔的問題

with t as (
    select 1 as msg_guid, 
    systimestamp - trunc(systimestamp) as diff 
    from dual 
) 
select diff, 
    trunc(1000 * (
    extract (day from diff) * (60*60*24) 
    + extract (hour from diff) * (60*60) 
    + extract (minute from diff) * 60 
    + extract (second from diff) 
)) as milliseconds 
from t; 

DIFF     MILLISECONDS 
---------------------- ------------ 
0 9:13:27.650365   33207650 

但是,也許你想作爲一個字符串,作爲獨立的部分組成:

with t as (
    select 1 as msg_guid, 
    systimestamp - trunc(systimestamp) as diff 
    from dual 
) 
select diff, 
    extract (day from diff) || ' DAYS ' 
    || extract (hour from diff) || ' HOURS ' 
    || extract (minute from diff) || ' MINUTES ' 
    || trunc(extract (second from diff)) || ' SECONDS ' 
    || (trunc(extract (second from diff) * 1000) 
     - (trunc(extract (second from diff)) * 1000)) || ' MILLISECONDS' 
    as text 
from t; 

DIFF     TEXT             
---------------------- ------------------------------------------------------- 
0 9:43:38.896007  0 DAYS 9 HOURS 43 MINUTES 38 SECONDS 896 MILLISECONDS 

SQL Fiddle根據您的樣本數據,排序,並用時間計算逆轉,因此該值是正的。

相關問題