2014-03-25 132 views
0

我在計算在一個作業的開始和結束時間之間花了多少時間。這裏是我的查詢:在oracle中減去兩個日期sql

SELECT request_id, user_concurrent_program_name, 
DECODE(phase_code,'C','Completed',phase_code) phase_code, 
DECODE(status_code,'D', 'Cancelled' , 
'E', 'Error' , 'G', 'Warning', 
'H','On Hold' , 'T', 'Terminating', 
'M', 'No Manager' , 'X', 'Terminated', 
'C', 'Normal', status_code) status_code, 
actual_start_date, actual_completion_date, completion_text 
FROM apps.fnd_conc_req_summary_v 
WHERE phase_code='C' AND 
status_code='C' 
ORDER BY 6 DESC 

actual_start_dateactual_completion_date的類型是DATE,格式爲象下面這樣:

ACTUAL_START_DATE   ACTUAL_COMPLETION_DATE 
3/25/2014 2:00:14 PM  3/25/2014 2:00:18 PM 

的問題是,我怎麼能減去這兩列,並得到類似00:00:04

在某些情況下,工作可以拼命地跑了兩天,我應該像2 days and 00:00:02

+2

[點擊這裏](http://stackoverflow.com/quest離子/ 15755714/oracle-query-the-difference-between-dates) – Himanshu

回答

1

以下方法提取的日子組件的結果,然後格式化使用to_char()時間:

select (trunc(ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE) || ' days and ' || 
     to_char(to_date('2000-01-01', 'YYYY-MM-DD') + (ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE), 
       'HH24:MI:SS' 
       ) 
     ) 

在你的主查詢,你想補充這給select列表的末尾:

SELECT request_id, user_concurrent_program_name, 
     DECODE(phase_code, 'C', 'Completed', phase_code) phase_code, 
     DECODE(status_code, 'D', 'Cancelled' , 'E', 'Error' , 'G', 'Warning', 
       'H','On Hold' , 'T', 'Terminating', 'M', 'No Manager' , 'X', 'Terminated', 
       'C', 'Normal', status_code 
      ) as status_code, 
     actual_start_date, actual_completion_date, completion_text 
     (trunc(ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE) || ' days and ' || 
     to_char(to_date('2000-01-01', 'YYYY-MM-DD') + (ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE), 
       'HH24:MI:SS' 
       ) 
     ) as Diff_DDHHMMSS 
FROM apps.fnd_conc_req_summary_v 
WHERE phase_code='C' AND 
status_code='C' 
ORDER BY 6 DESC; 
+0

如何將此sql查詢集成到我的主查詢中? – AloneInTheDark

+0

這種工作,但有一個問題,「小時」部分總是顯示值「12」,像這樣:「0天和12:00:01」。 – AloneInTheDark

+0

@AloneInTheDark。 。 。當然。 Oracle應該是'HH24'。 –