Removing all rows from the database except the first tow lines in oracle

Hello, I have a problem when deleting rows, I want to save two rows in the database and delete all the other rows, I found the query in this link for the delete row but it gives the mysql syntax restriction function. but in oracle there is no limit variable available. how can i do the same

DELETE FROM Table
  WHERE DateColumn NOT IN (SELECT DateColumn 
                             FROM Table 
                         GROUP BY date(DateColumn) 
                         ORDER BY DateColumn DESC
                            LIMIT 2);
+4
source share
3 answers

This cabin can be reached with this request.

DELETE FROM TABLE 
 WHERE ROWID NOT IN
    (SELECT ROWIDS FROM (SELECT MIN(ROWID) ROWIDS 
                           FROM TABLE 
                          GROUP BY DATECOLUMN
                          ORDER BY DATECOLUMN DESC)
      WHERE ROWNUM <= 2);

The inner query itself will return min rowid from the table, and then the immediate outer query will select only two rows that should be avoided at the most remote delete.

+1

ROWID, ROWNUM - ( )

delete from table WHERE rowid NOT IN
    (SELECT rowid FROM (SELECT rowid
                          FROM Table 
                         ORDER BY DateColumn DESC)
     WHERE rownum <= 2);

, "" , rownum, order by, rownum

, (),

delete from table WHERE rowid NOT IN
    (SELECT rowid FROM table WHERE rownum <= 2);
+2
DELETE FROM Table
  WHERE DateColumn NOT IN (SELECT DateColumn 
                             FROM Table 
                             Where ROWNUM < 3
                             GROUP BY date(DateColumn) 
                             ORDER BY DateColumn DESC
                            );

I do not have oracle client to run the request. you can use ROWNUM in Oracle since LIMIT is not available in ORACLE

0
source

Source: https://habr.com/ru/post/1536404/


All Articles