如何在oracle上删除表的奇数行?

时间:2017-03-07 14:51:27

标签: oracle oracle11g

我有一张表可以保存某些车辆的GPS传输。每30秒车辆传送他的位置。 现在我需要每1分钟传输一次。

我原以为我可以用奇数rownum删除每一行。 ¿如何删除表格的奇数行?

也许有些车辆每30秒没发送一次,我想检查一下。 ¿我怎样才能检查每30秒钟是否确实有传输?

这只是表格的一小部分:

DET_ID    DATE_TIME              LATI       LONG
--------- ---------------------- ---------- ----------
     3412 15-DEC-16 11.05.09 AM  11.42341   -81.95251
     3412 15-DEC-16 11.05.39 AM  11.42344   -81.92435
     3412 15-DEC-16 11.06.09 AM  11.43567   -81.85355
     3412 15-DEC-16 11.06.39 AM  11.47851   -81.95246
     3412 15-DEC-16 11.07.10 AM  11.45123   -81.63451
     3412 15-DEC-16 11.07.40 AM  11.43451   -81.95351
     3412 15-DEC-16 11.08.10 AM  11.45251   -81.46785
     3412 15-DEC-16 11.08.40 AM  11.45678   -81.95551
     3412 15-DEC-16 11.09.10 AM  11.43451   -81.93241
     3412 15-DEC-16 11.09.40 AM  11.45456   -81.95251
     3412 15-DEC-16 11.10.10 AM  11.42890   -81.95451
     3412 15-DEC-16 11.15.40 AM  11.43451   -81.95351
     3412 15-DEC-16 11.16.10 AM  11.45251   -81.46785
     3412 15-DEC-16 11.16.40 AM  11.45678   -81.95551
     3412 15-DEC-16 11.17.10 AM  11.43451   -81.93241
     3412 15-DEC-16 11.17.40 AM  11.45456   -81.95251
     3412 15-DEC-16 11.18.10 AM  11.42890   -81.95451

更新: 使用Mt0的查询,我发现我有几个间隙,因此删除奇数行不是一个好主意。

如何在同一分钟内删除多于1个值的行(每分钟只留一个值)?

3 个答案:

答案 0 :(得分:1)

  

如何删除表格的奇数行?

如果您只想删除上半部分的那些条目,那么:

Sub Completed_Move()
    ' Completed_Move
    Macro Dim srchrng As Range, row As Range

    Set srchrng = Range("H1,H500")
    'For each cel in srchrng
    If InStr(1, cel.Value) = Complete Then
        Activecell.Offset(0, -7).Range("A1:N1").Select
        Selection.Cut
        Sheets("Completed").Select
        Activecell.Select
        ActiveSheet.Paste Sheets("Current").Select
        Activecell.Rows("2:1").EntireRow.Select
        Selection.Delete Shift:=xlUp
        Activecell.Offset(2, 7).Range("A1").Select
    End if
End Sub

或者,这将删除同一分钟中存在较早条目的行:

DELETE FROM your_table
WHERE  ( date_time - TRUNC( date_time, 'MI' ) ) * 24*60*60 < 30;
  

如何检查每30秒是否确实有传输?

这将选择前一行之后29到31秒之间的所有行:

DELETE FROM your_table
WHERE ROWID IN (
  SELECT ROWID
  FROM   (
    SELECT ROW_NUMBER() OVER ( PARTITION BY det_id, TRUNC( date_time, 'MI' )
                               ORDER BY date_time ) AS rn
    FROM   your_table
  )
  WHERE rn > 1
);

答案 1 :(得分:0)

要删除每个组中的所有行,您可以使用row_number()功能。例如:

create table a(n number);
insert into a values(1.1);
insert into a values(1.3);
insert into a values(1.2);
insert into a values(2.1);
insert into a values(2.5);
insert into a values(3.9);
insert into a values(3.9);
insert into a values(5.5);
insert into a values(6.1);
insert into a values(6.3);
commit;

select * from a;
N
1,1
1,3
1,2
2,1
2,5
3,9
3,9
5,5
6,1
6,3

delete from a
 where rowid in (select rowid
                   from (select rowid,
                                n,
                                row_number() over(partition by trunc(n) order by n) rn
                           from a)
                  where rn > 1);


select * from a;
N
1,1
2,1
3,9
5,5
6,1

您可以使用生成的时间序列在传输系列中找到漏洞

with generator as
 (select rownum rn from dual connect by rownum <= 6)
select * from generator left join a on generator.rn = trunc(a.n) where a.n is null order by rn

RN N
4 -

答案 2 :(得分:0)

希望这足以满足您的要求。

CREATE TABLE TEST_DUP
  (
    DET_ID    NUMBER,
    DATE_TIME TIMESTAMP,
    LATTI     number,
    LONGI      NUMBER
  );

--Insert Values into table

-- This query will just keep only one record for every minute.
    DELETE
    FROM TEST_DUP
    WHERE ROWID IN
      (SELECT a.RID
      FROM
        (SELECT ROWID rid,
          ROW_NUMBER() OVER(PARTITION BY DET_ID,TO_CHAR(DATE_TIME,'MM/DD/YY HH24:MI') ORDER BY 2) RN
        FROM TEST_DUP
        )a
      WHERE a.rn > 1
      );