英文:
SQL: delete records from the table A when id is less than equal to maximum id of a group in table B
问题
我需要删除arh表中的记录,对于每个accountMappingId,其中actionRequestId <= ar表中相应mappingId的"RESTORE"操作的最大actionRequestId。这些记录应仅在arh表中删除,不影响ar表。
已尝试的解决方案是:
delete from arh
where (arh.actionRequestId, arh.accountMappingId) in (
select arh.actionRequestId, new_table.mappingId
from arh
inner join (
SELECT mappingId, max(actionrequestid) as actionrequestid
FROM ar
where operation = 'RESTORE'
group by mappingid
)
as new_table
on arh.accountMappingId = new_table.mappingId
and arh.actionRequestId <= new_table.actionRequestId
order by arh.actionRequestId, new_table.mappingId)
是否可以优化删除查询?
英文:
I have 2 tables arh and ar .I need to delete records from arh table for each accountMappingId where actionRequestId is <= maximum actionRequestId for the operation 'RESTORE' on the equivalent mappingId in ar table. The records should be deleted only in arh table without affecting ar table.
There can be different accountMappingId (1001, 1002, 1003).
-- create table --
create table arh (
accountMappingId TEXT,
actionRequestId integer,
actionrequesthistoryid integer
);
CREATE TABLE ar (
mappingId TEXT,
actionRequestId integer,
operation TEXT
);
-- insert records ---
INSERT INTO arh VALUES ('1001', 1, 1);
INSERT INTO arh VALUES ('1001', 1, 2);
INSERT INTO arh VALUES ('1001', 2, 1);
INSERT INTO arh VALUES ('1001', 2, 2);
INSERT INTO arh VALUES ('1001', 3, 1);
INSERT INTO arh VALUES ('1001', 3, 2);
INSERT INTO arh VALUES ('1001', 4, 1);
INSERT INTO arh VALUES ('1001', 4, 2);
INSERT INTO arh VALUES ('1001', 5, 1);
INSERT INTO arh VALUES ('1001', 5, 2);
INSERT INTO arh VALUES ('1001', 6, 1);
INSERT INTO arh VALUES ('1001', 6, 2);
INSERT INTO arh VALUES ('1001', 7, 1);
INSERT INTO arh VALUES ('1001', 7, 2);
INSERT INTO arh VALUES ('1001', 8, 1);
INSERT INTO arh VALUES ('1001', 8, 2);
INSERT INTO arh VALUES ('1002', 1, 1);
INSERT INTO arh VALUES ('1002', 1, 2);
INSERT INTO arh VALUES ('1002', 2, 1);
INSERT INTO arh VALUES ('1002', 2, 2);
INSERT INTO arh VALUES ('1002', 3, 1);
INSERT INTO arh VALUES ('1002', 3, 2);
INSERT INTO ar VALUES ('1001', 1, 'COPY');
INSERT INTO ar VALUES ('1001', 2, 'REMOVE');
INSERT INTO ar VALUES ('1001', 3, 'RESTORE');
INSERT INTO ar VALUES ('1001', 4, 'COPY');
INSERT INTO ar VALUES ('1001', 5, 'REMOVE');
INSERT INTO ar VALUES ('1001', 6, 'RESTORE'); --> max(actionrequestId) for 'RESTORE' operation in '1001'
INSERT INTO ar VALUES ('1001', 7, 'COPY');
INSERT INTO ar VALUES ('1001', 8, 'REMOVE');
INSERT INTO ar VALUES ('1002', 1, 'COPY');
INSERT INTO ar VALUES ('1002', 2, 'REMOVE');
INSERT INTO ar VALUES ('1002', 3, 'RESTORE'); --> max(actionrequestId) for 'RESTORE' operation in '1002'
Solution what I have tried but can we optimize the delete query?
delete from arh
where (arh.actionRequestId, arh.accountMappingId) in (
select arh.actionRequestId, new_table.mappingId
from arh
inner join (
SELECT mappingId, max(actionrequestid) as actionrequestid
FROM ar
where operation = 'RESTORE'
group by mappingid
)
as new_table
on arh.accountMappingId = new_table.mappingId
and arh.actionRequestId <= new_table.actionRequestId
order by arh.actionRequestId, new_table.mappingId)
答案1
得分: 2
使用子查询获取映射ID的最大还原请求ID。
DELETE FROM arh
WHERE actionRequestId <= (SELECT MAX(ar.actionRequestId)
FROM ar
WHERE ar.operation = 'RESTORE'
AND ar.mappingId = arh.accountMappingId);
演示链接: https://dbfiddle.uk/Frx1Ea3_
英文:
Use a subquery to get the Mapping ID's maximum RESTORE request ID.
DELETE FROM arh
WHERE actionRequestId <= ( SELECT MAX(ar.actionRequestId)
FROM ar
WHERE ar.operation = 'RESTORE'
AND ar.mappingId = arh.accountMappingId );
Demo: https://dbfiddle.uk/Frx1Ea3_
答案2
得分: 1
你可以从ar表中找到max(actionRequestId)
,条件是operation = 'RESTORE'
,然后使用USING
子句从arh表中删除所需的行。
delete from arh T
using
(
select mappingId, max(actionRequestId) max_actionRequestId
from ar
where operation = 'RESTORE'
group by mappingId
) D
where T.accountMappingId = D.mappingId and
T.actionRequestId <= D.max_actionRequestId
英文:
You could find the max(actionRequestId)
from ar table where operation = 'RESTORE'
, then use the USING
clause to delete the desired rows from arh table.
delete from arh T
using
(
select mappingId, max(actionRequestId) max_actionRequestId
from ar
where operation = 'RESTORE'
group by mappingId
) D
where T.accountMappingId = D.mappingId and
T.actionRequestId <= D.max_actionRequestId
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论