2011-12-08 24 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