2011-12-08 48 views
3

我正嘗試使用SQL根據一個條目和下一個條目之間的時間差異來選擇不同的數據條目。它更容易用一個例子來解釋:在時間範圍之外選擇不同的行

我的數據表有

Part DateTime 
123  12:00:00 
123  12:00:05 
123  12:00:06 
456  12:10:23 
789  12:12:13 
123  12:14:32 

我想與限制,只要返回所有行,如果有使用相同的「部分」號的多個條目我會喜歡只檢索那些至少5分鐘的差異。

查詢應返回:

Part DateTime 
123  12:00:00 
456  12:10:23 
789  12:12:13 
123  12:14:32 

我正在使用的代碼如下:

SELECT data1.*, to_char(data1.scan_time, 'yyyymmdd hh24:mi:ss') 

FROM data data1 

where exists 
(
    select * 

    from data data2 

    where data1.part_serial_number = data2.part_serial_number AND 
    data2.scan_time + 5/1440 >= data1.scan_time 
    and data2.info is null 
) 

order by to_char(data1.scan_time, 'yyyymmdd hh24:mi:ss'), data1.part_serial_number 

這不是遺憾的是工作。有誰知道我做錯了什麼或可以建議一種替代方法?

由於

+0

我想知道你是否試過我的方法。這是否過於簡單? – MPelletier

回答

3

救援分析功能。

您可以使用分析函數LEAD來獲取零件下一行的數據。

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (
    2 select 123 part, timestamp '2011-12-08 00:00:00' ts 
    3  from dual 
    4 union all 
    5 select 123, timestamp '2011-12-08 00:00:05' 
    6  from dual 
    7 union all 
    8 select 123, timestamp '2011-12-08 00:00:06' 
    9  from dual 
10 union all 
11 select 456, timestamp '2011-12-08 00:10:23' 
12  from dual 
13 union all 
14 select 789, timestamp '2011-12-08 00:12:13' 
15  from dual 
16 union all 
17 select 123, timestamp '2011-12-08 00:14:32' 
18  from dual 
19 ) 
20 select part, 
21   ts, 
22   lead(ts) over (partition by part order by ts) next_ts 
23* from x 
SQL>/

     PART TS        NEXT_TS 
---------- ------------------------------- ------------------------------- 
     123 08-DEC-11 12.00.00.000000000 AM 08-DEC-11 12.00.05.000000000 AM 
     123 08-DEC-11 12.00.05.000000000 AM 08-DEC-11 12.00.06.000000000 AM 
     123 08-DEC-11 12.00.06.000000000 AM 08-DEC-11 12.14.32.000000000 AM 
     123 08-DEC-11 12.14.32.000000000 AM 
     456 08-DEC-11 12.10.23.000000000 AM 
     789 08-DEC-11 12.12.13.000000000 AM 

6 rows selected. 

一旦你做到了這一點,那麼你可以創建一個內嵌視圖,只需選擇那些行下一個日期是當前日期後超過5分鐘。

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (
    2 select 123 part, timestamp '2011-12-08 00:00:00' ts 
    3  from dual 
    4 union all 
    5 select 123, timestamp '2011-12-08 00:00:05' 
    6  from dual 
    7 union all 
    8 select 123, timestamp '2011-12-08 00:00:06' 
    9  from dual 
10 union all 
11 select 456, timestamp '2011-12-08 00:10:23' 
12  from dual 
13 union all 
14 select 789, timestamp '2011-12-08 00:12:13' 
15  from dual 
16 union all 
17 select 123, timestamp '2011-12-08 00:14:32' 
18  from dual 
19 ) 
20 select part, 
21   ts 
22 from (
23  select part, 
24    ts, 
25    lead(ts) over (partition by part order by ts) next_ts 
26  from x) 
27 where next_ts is null 
28*  or next_ts > ts + interval '5' minute 
SQL>/

     PART TS 
---------- ------------------------------- 
     123 08-DEC-11 12.00.06.000000000 AM 
     123 08-DEC-11 12.14.32.000000000 AM 
     456 08-DEC-11 12.10.23.000000000 AM 
     789 08-DEC-11 12.12.13.000000000 AM 
+0

哇,謝謝!不知道這個方便的主角功能 – AFJ

0

這尚未得到驗證,但本質上,關鍵是要由組部分和時間由5分鐘劃分(地板)。

select part, min(scan_time) 
from data 
group by part, floor(scan_time/(5/1440)) 
order by scan_time; 
1

AFJ,

,我們有一個新的領域,如果存在於前5分鐘本部previus條目,告訴我們,讓我們supose,然後,取行此字段設置爲False我們有結果。

select 
    Part, 
    DateTime, 
    coalesce(
    (select distinct 1 
    from data ds 
    where ds.Part = d.Part 
     and ds.DateTime between d.DateTime and d.DateTime - 5/1440 
    ) 
    , 0) as exists_previous 
from data d 

子查詢檢查它們是否與以前5分鐘相同部分的排inteval

結果必然是:

Part DateTime exists_previous 
123  12:00:00 0 
123  12:00:05 1 
123  12:00:06 1 
456  12:10:23 0 
789  12:12:13 0 
123  12:14:32 0 

現在,過濾得到0只行:

select Part, DateTime from 
    (select 
     Part, 
     DateTime, 
     coalesce(
     (select distinct 1 
     from data ds 
     where ds.Part = d.Part 
      and ds.DateTime between d.DateTime and d.DateTime - 5/1440 
     ) 
     , 0) as exists_previous 
    from data D 
    ) T where T.exists_previous = 0 

聲明:未經測試。

+0

謝謝danihp。我瞭解您的查詢是如何工作的,但是我無法讓子查詢正常工作。它在exists_previous列中返回所有具有「0」的行。 – AFJ