2011-04-28 52 views
1

我的第一臺TIMINGS就像我想要得到時間的Oracle查詢範圍

EMPCODE,Att_DATE,IN_TIME,OUT_TIME 

MAIN

EMPCODE,DESIGANTION,LACATION ETC. 

的數據在我TIMINGS表是這樣的:

10101 7/2/2010 7/2/2011 7:08:31 AM  7/2/2011 12:05:31 PM 
10101 7/2/2010 7/2/2011 12:58:07 PM 7/2/2011 1:47:36 PM 
10101 7/2/2010 7/2/2011 2:17:52 PM  7/2/2011 4:37:23 PM
10102 7/2/2010 7/2/2011 9:44:07 AM  7/2/2011 10:17:53 AM 
10102 7/2/2010 7/2/2011 10:25:21 AM 7/2/2011 11:18:28 AM 
10102 7/2/2010 7/2/2011 11:28:32 AM 7/2/2011 12:10:31 PM 
10102 7/2/2010 7/2/2011 12:35:15 PM  7/2/2011 04:10:04 PM
10103 7/2/2010 7/2/2011 8:56:52 AM  7/2/2011 10:03:39 AM 
10103 7/2/2010 7/2/2011 10:10:02 AM 7/2/2011 12:00:07 PM 
10103 7/2/2010 7/2/2011 12:22:54 PM 7/2/2011 4:11:03 PM 
10103 7/2/2011 7/2/2011 4:51:45 PM  7/2/2011 5:10:45 PM
10104 7/2/2010 7/2/2011 5:12:37 PM  7/2/2011 6:08:23 PM 
10104 7/2/2010 7/2/2011 6:20:47 PM  7/3/2011 5:04:33 AM

然後我想在以下定時對

from-time to-time 
00:00:00  7:08 AM 
12:10:31PM 12:22:54PM 
4:37:23PM 4:51:45PM
+1

歡迎來到StackOverflow!當你問你的問題時,右邊有這個方便的**如何格式**框。值得一讀,就像問題區域上方** [?] **那樣[鏈接的頁面](http://stackoverflow.com/editing-help)。我已經爲你做了一些格式化,但你的問題仍然不清楚(特別是最後似乎還有一段不完整的句子)。你的問題越清晰,你所得到的答案的質量和數量就越好。最好, – 2011-04-28 07:31:33

+0

您能否澄清一下您想要在輸出中獲得什麼? – andr 2011-04-28 08:13:54

+1

您還希望看到5:10:45 PM到5:12:37 PM和6:08:23 PM到6:20:47 PM? – 2011-04-28 08:27:07

回答

2

查詢像這樣的內而外最好內置於顯示輸出作爲

NO在OFC一個存在。

首先你需要一個結果集,它只列出時間和人的變化。

SELECT to_date('2010/07/02', 'yyyy/mm/dd') as event_time 
    , 0 as change 
    FROM dual 
UNION ALL 
    SELECT IN_TIME as event_time 
    , 1 as change 
    FROM timings 
    WHERE att_date = to_date('2010/07/02', 'yyyy/mm/dd') 
UNION ALL 
    SELECT OUT_TIME as event_time 
    , -1 as change 
    FROM timings 
    WHERE att_date = to_date('2010/07/02', 'yyyy/mm/dd') 

接下來你需要間隔和運行總數。 Oracle有此機制,詳情請參閱http://www.orafaq.com/node/55

SELECT event_time as interval_start 
    , lead(event_time, 1, to_date('2010/07/03', 'yyyy/mm/dd') 
    OVER (ORDER BY event_time) as interval_end 
    , sum(change) OVER (ORDER BY event_time) 
    ROWS BETWEEN (UNBOUNDED PRECEDING AND CURRENT ROW) as people 
FROM (
    previous query here 
) 

這會給你的人和間隔。你現在只需要過濾掉。

SELECT interval_start, interval_end 
FROM (
    previous query here 
) 
WHERE people = 0;