且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除按三列分组并按日期排序的旧记录

更新时间:2023-01-30 08:17:41

使用ROW_NUMBER解析函数找出每组中不是最新的行,然后可以使用DELETE 中关联的 ROWID 伪列:

Use the ROW_NUMBER analytic function to find the rows that are not the latest in each group and then you can use the ROWID pseudo-column to correlate with in the DELETE:

DELETE FROM tmp_data
WHERE ROWID IN (
  SELECT rid
  FROM   (
    SELECT ROWID As rid,
           ROW_NUMBER() OVER (
             PARTITION BY col_2, col_3, col_6
             ORDER BY col_4 DESC
           ) AS rn
    FROM   tmp_data
  )
  WHERE rn > 1
)

删除 22 行.

db<>fiddle 这里