删除按三列分组并按日期排序的旧记录
remove olde records grouping by three columns and ordering by date
情况是这样的:
CREATE SEQUENCE SEQ_tmpdata MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2206 NOCACHE ORDER NOCYCLE NOKEEP NOSCALE GLOBAL;
CREATE TABLE TMP_DATA (
ID NUMBER(19,0) DEFAULT SEQ_tmpdata.nextval NOT NULL
,COL_1 VARCHAR2(256 CHAR)
,COL_2 VARCHAR2(256 BYTE)
,COL_3 VARCHAR2(256 BYTE)
,COL_4 TIMESTAMP(6)
,COL_5 NUMBER(19,6)
,COL_6 VARCHAR2(256 BYTE)
);
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','MB', TO_TIMESTAMP('2020-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,3141','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','RC', TO_TIMESTAMP('2020-06-18 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '15,8686','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','57', TO_TIMESTAMP('2020-02-19 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,362','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE32','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '29,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','AS', TO_TIMESTAMP('2020-05-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,5556','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P7','NO_VALUE', TO_TIMESTAMP('2020-04-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2568','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A02','RA', TO_TIMESTAMP('2020-05-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '4,4584','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE33','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '180','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','38', TO_TIMESTAMP('2020-02-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,6657','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','18', TO_TIMESTAMP('2020-02-25 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,5658','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE34','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '260','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RA', TO_TIMESTAMP('2020-05-08 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,127','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P7','NO_VALUE', TO_TIMESTAMP('2020-04-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2568','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-04-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,0588','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE35','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '460','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','MB', TO_TIMESTAMP('2020-06-18 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '11,2971','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','NO_VALUE', TO_TIMESTAMP('2020-04-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '16,7299','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE36','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '220','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','38', TO_TIMESTAMP('2020-02-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,6657','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','48', TO_TIMESTAMP('2020-04-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,2402','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','30', TO_TIMESTAMP('2020-03-27 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,4043','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P10','MB', TO_TIMESTAMP('2020-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '15,0002','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','12', TO_TIMESTAMP('2020-04-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,8953','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A10','NO_VALUE', TO_TIMESTAMP('2020-04-14 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,9176','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','NO_VALUE', TO_TIMESTAMP('2020-03-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,336','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','18', TO_TIMESTAMP('2020-02-25 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,5658','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','57', TO_TIMESTAMP('2020-02-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '4,2724','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '12,2274','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A10','NO_VALUE', TO_TIMESTAMP('2020-04-14 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,9176','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE37','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '229,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','8', TO_TIMESTAMP('2020-04-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,8931','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RC', TO_TIMESTAMP('2020-06-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,9942','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','49', TO_TIMESTAMP('2020-04-06 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2022','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RC', TO_TIMESTAMP('2020-06-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,9942','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','12', TO_TIMESTAMP('2020-04-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,8953','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-04-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,0588','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,6923','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE38','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '209,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE38','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '209,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '12,2274','tabla_tmp_4');
commit;
问题是:
我需要删除旧记录,按 col_2、col_3 和 col_6 分组并按 col_4 排序。要让最后一个元素下注 col_6.
想法是这样的:
我测试这个只是为了测试删除 'tabla_tmp_1':
的旧记录
delete tmp_data f
where col_4 not in (
select max(col_4)
from tmp_data s
where f.col_1 = s.col_1
and f.col_2 = s.col_2
and f.col_3 = s.col_3
and col_6 = 'tabla_tmp_1'
and col_1 = 'phone'
and col_4 < to_date('27/09/2020','DD/MM/YYYY')
);
但不起作用。
有人可以帮我解决这个问题吗?
此致
使用ROW_NUMBER
解析函数找出每组中不是最新的行然后可以使用ROWID
pseudo-column在[=13中进行关联=]:
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 here
情况是这样的:
CREATE SEQUENCE SEQ_tmpdata MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2206 NOCACHE ORDER NOCYCLE NOKEEP NOSCALE GLOBAL;
CREATE TABLE TMP_DATA (
ID NUMBER(19,0) DEFAULT SEQ_tmpdata.nextval NOT NULL
,COL_1 VARCHAR2(256 CHAR)
,COL_2 VARCHAR2(256 BYTE)
,COL_3 VARCHAR2(256 BYTE)
,COL_4 TIMESTAMP(6)
,COL_5 NUMBER(19,6)
,COL_6 VARCHAR2(256 BYTE)
);
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','MB', TO_TIMESTAMP('2020-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,3141','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','RC', TO_TIMESTAMP('2020-06-18 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '15,8686','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','57', TO_TIMESTAMP('2020-02-19 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,362','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE32','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '29,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','AS', TO_TIMESTAMP('2020-05-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,5556','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P7','NO_VALUE', TO_TIMESTAMP('2020-04-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2568','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A02','RA', TO_TIMESTAMP('2020-05-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '4,4584','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE33','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '180','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','38', TO_TIMESTAMP('2020-02-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,6657','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','18', TO_TIMESTAMP('2020-02-25 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,5658','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE34','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '260','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RA', TO_TIMESTAMP('2020-05-08 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,127','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P7','NO_VALUE', TO_TIMESTAMP('2020-04-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2568','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-04-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,0588','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE35','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '460','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','MB', TO_TIMESTAMP('2020-06-18 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '11,2971','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','NO_VALUE', TO_TIMESTAMP('2020-04-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '16,7299','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE36','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '220','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','38', TO_TIMESTAMP('2020-02-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,6657','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','21', TO_TIMESTAMP('2020-03-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '6,1776','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','48', TO_TIMESTAMP('2020-04-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,2402','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','30', TO_TIMESTAMP('2020-03-27 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,4043','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P10','MB', TO_TIMESTAMP('2020-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '15,0002','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','12', TO_TIMESTAMP('2020-04-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,8953','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A10','NO_VALUE', TO_TIMESTAMP('2020-04-14 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,9176','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','NO_VALUE', TO_TIMESTAMP('2020-03-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '14,336','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','18', TO_TIMESTAMP('2020-02-25 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,5658','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','45', TO_TIMESTAMP('2020-05-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '8,5812','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P9','57', TO_TIMESTAMP('2020-02-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '4,2724','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '12,2274','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','A10','NO_VALUE', TO_TIMESTAMP('2020-04-14 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,9176','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE37','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '229,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RA', TO_TIMESTAMP('2020-06-29 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '2,555','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P11','8', TO_TIMESTAMP('2020-04-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,8931','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RC', TO_TIMESTAMP('2020-06-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,9942','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P4','49', TO_TIMESTAMP('2020-04-06 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '7,2022','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','RC', TO_TIMESTAMP('2020-06-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,9942','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P8','12', TO_TIMESTAMP('2020-04-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,8953','tabla_tmp_1');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-04-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '9,0588','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '5,6923','tabla_tmp_4');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE38','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '209,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('NO_VALUE','DE38','NO_VALUE', TO_TIMESTAMP('2019-06-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '209,32','tabla_tmp_3');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P3','RC', TO_TIMESTAMP('2020-06-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '13,7008','tabla_tmp_2');
insert into tmp_data (col_1, col_2, col_3, col_4, col_5, col_6) values ('phone','P1','NO_VALUE', TO_TIMESTAMP('2020-03-26 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), '12,2274','tabla_tmp_4');
commit;
问题是: 我需要删除旧记录,按 col_2、col_3 和 col_6 分组并按 col_4 排序。要让最后一个元素下注 col_6.
想法是这样的:
我测试这个只是为了测试删除 'tabla_tmp_1':
的旧记录delete tmp_data f
where col_4 not in (
select max(col_4)
from tmp_data s
where f.col_1 = s.col_1
and f.col_2 = s.col_2
and f.col_3 = s.col_3
and col_6 = 'tabla_tmp_1'
and col_1 = 'phone'
and col_4 < to_date('27/09/2020','DD/MM/YYYY')
);
但不起作用。
有人可以帮我解决这个问题吗?
此致
使用ROW_NUMBER
解析函数找出每组中不是最新的行然后可以使用ROWID
pseudo-column在[=13中进行关联=]:
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 here