简体   繁体   中英

Oracle SQL Developer comparing rows

I am using Oracle SQL Developer, Version 4.0.0.13, Build MAIN-13.80

I have interesting problem, my query returns to me big amount of data with priority (1 to 4) and date/time. What I have to accomplish is to delete all rows from table that has priority 4 AND that next event that occurred was in 5 minutes time frame from previous with priority 1-3.

Maybe it sounds confusing, but see data below

           Date            Priority 
 - 2013-03-28 21:42:08        4 

 - 2013-03-28 21:45:20        1

 - 2013-03-28 21:45:16        1 

 - 2013-03-28 21:46:37        1

So from here, I would like to delete first row, because it has priority 4 and because next event that happen was in 5 minute time frame and it was in (1,2,3). In case next row was also priority 4, we just ignore it and move on.

In table I have column for Date , Priority and RowID so how can I check row after priority 4 and compare dates to see 5 minutes difference, and of course everything is sorted by date asc?

Thank you

It sounds like you want something like

DELETE FROM table_name a
 WHERE priority = 4
   AND EXISTS( SELECT 1
                 FROM table_name b
                WHERE b.priority IN (1,2,3)
                  AND b.id != a.id
                  AND b.dt BETWEEN a.dt AND a.dt + interval '5' minute )

I'm assuming that your column names aren't actually rowid and date since those are reserved words. I'm using id and dt instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM