sql - PostgreSQL 删除每个组中除最新条目之外的所有条目

标签 sql postgresql

在管理任务中,我需要清理所有行(每个实体),但不是最新的。我目前看不到没有循环的方法。

当前的数据源是这样的:

enter image description here

我应该删除“旧的”执行并只保留最新的。

enter image description here

我的方法是循环遍历实体键,但我想避免对每个键进行循环。最后,结果应该是要删除的 task_id 和 taskexec_id 的列表。 有没有办法只使用 postgre/sql 来做到这一点?

这是我目前所拥有的:

create function entity_with_multiple_propagationtasks()
returns TABLE(entitykey character varying)
language plpgsql
as
$$
BEGIN
    RETURN QUERY SELECT DISTINCT task.entitykey FROM
        (SELECT  task.entitykey FROM task WHERE dtype = 'PropagationTask' GROUP BY task.entitykey having count(*) > (SELECT count(*) FROM conninstance)) more_than_one_entry
            INNER JOIN task ON task.entitykey = more_than_one_entry.entitykey
            INNER JOIN taskexec ON taskexec.task_id = task.id ORDER BY task.entitykey ASC;
END
$$;

SELECT task.entitykey AS entitykey, task.id AS task_id, taskexec.id AS taskexec_id, taskexec.enddate as enddate
FROM task
         JOIN taskexec ON taskexec.task_id = task.id
         JOIN entity_with_multiple_propagationTasks() AS mt ON mt.entitykey = task.entitykey
WHERE task.dtype = 'PropagationTask'
group by task.entitykey, task.id, taskexec.id
ORDER BY task.entitykey asc , taskexec.enddate desc

示例数据

CREATE TABLE MY_TABLE(entitykey varchar, task_id varchar, taskexec_id varchar, enddate varchar)

INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('2910b47e-b228-4fa8-90b4-7eb2289fa81e', 'f604d8ef-dc11-4a20-84d8-efdc11fa20db', 'c03756f3-4e2c-4bc3-b756-f34e2c7bc3c3', '2019-10-21 18:57:34.771000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('2910b47e-b228-4fa8-90b4-7eb2289fa81e', '67dc7946-bb1a-4db1-9c79-46bb1a4db136', '2ef21bb0-0070-40d7-b21b-b0007000d752', '2019-10-21 18:57:19.260000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('2910b47e-b228-4fa8-90b4-7eb2289fa81e', '65449dba-4361-4c77-849d-ba43610c770b', '53c8a2c8-acc0-47f0-88a2-c8acc097f05c', '2019-10-21 18:57:03.823000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('2910b47e-b228-4fa8-90b4-7eb2289fa81e', 'f1dfc360-a29a-41b4-9fc3-60a29a11b46a', '7e506871-2080-42c0-9068-712080d2c096', '2019-10-21 18:56:48.300000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('2910b47e-b228-4fa8-90b4-7eb2289fa81e', 'f8acd270-bdd8-46f3-acd2-70bdd856f349', '4aee0d9e-b3f9-4755-ae0d-9eb3f9d7554d', '2019-10-21 18:56:30.758000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d54eea25-7147-4a49-8eea-2571472a4902', '177b41c0-cad8-49c4-bb41-c0cad829c4cd', '177a7de0-2043-4fd8-ba7d-e020431fd846', '2019-10-21 18:57:34.817000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d54eea25-7147-4a49-8eea-2571472a4902', '8b2ee3e7-c2e3-43b1-aee3-e7c2e303b157', '3dfc2db1-aec9-4a2b-bc2d-b1aec9da2bfd', '2019-10-21 18:57:19.268000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d54eea25-7147-4a49-8eea-2571472a4902', '9abf5e45-eda1-4c54-bf5e-45eda1cc54dc', 'add16b75-b012-4c34-916b-75b012bc34b5', '2019-10-21 18:57:03.859000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d54eea25-7147-4a49-8eea-2571472a4902', '9f686e2f-04bc-4ced-a86e-2f04bc0ced84', '30e37365-968f-4131-a373-65968f1131c6', '2019-10-21 18:56:48.242000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d54eea25-7147-4a49-8eea-2571472a4902', 'f52cc7af-36a9-459a-acc7-af36a9259af5', '1e8d001e-5f22-41ec-8d00-1e5f2201ecce', '2019-10-21 18:56:30.764000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d8a3a959-8dca-4055-a3a9-598dca60555a', 'a05f016c-40e7-4ba7-9f01-6c40e7eba7c1', 'ec9b1822-1dce-4b6f-9b18-221dce9b6f06', '2019-10-21 18:57:34.795000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d8a3a959-8dca-4055-a3a9-598dca60555a', 'b6019b07-5d22-4c25-819b-075d222c252f', '53dbadd9-a6b3-46d1-9bad-d9a6b336d107', '2019-10-21 18:57:19.272000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d8a3a959-8dca-4055-a3a9-598dca60555a', '21850d53-be79-4099-850d-53be79109956', '9f3255d5-4623-4aa1-b255-d54623caa1ea', '2019-10-21 18:57:03.831000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d8a3a959-8dca-4055-a3a9-598dca60555a', '4c5ee68a-76f2-4d53-9ee6-8a76f26d5365', '2a4583a1-46c7-4374-8583-a146c72374f0', '2019-10-21 18:56:48.222000');
INSERT INTO  MY_TABLE(entitykey, task_id, taskexec_id, enddate) VALUES ('d8a3a959-8dca-4055-a3a9-598dca60555a', '83046850-1cfa-4f6c-8468-501cfa6f6c23', 'ab481793-8684-430f-8817-938684530f0d', '2019-10-21 18:56:30.758000');

最佳答案

可以使用一个简单的 EXISTS 查询来查找所有值得删除的行(存在较新行的行):

SELECT * -- replace with DELETE
FROM MY_TABLE AS todel
WHERE EXISTS (
    SELECT 1
    FROM MY_TABLE AS newer
    WHERE newer.entitykey = todel.entitykey
    AND newer.enddate > todel.enddate
)

关于sql - PostgreSQL 删除每个组中除最新条目之外的所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58499335/

相关文章:

postgresql - 如何强制前缀 "pg_"在 postgres 中创建新用户?

mysql - 内连接根据日期仅从第二个表中选择一行

sql - 在许多表中找到关系

mysql - 如何将 like 与特定模式一起使用?

java - 使用 JDBI 将二维数组插入 PostreSQL DB?

sql - PostgreSQL 到_日期()

sql - 错误: "coalesce" is not a known variable in INTO clause

python - 排除基于另一个没有外键的模型的值的模型

php - MySQL '=' 运算符没有返回结果

SQL Server - 分组依据 - 附加列