【发布时间】:2021-12-21 07:11:52
【问题描述】:
我有一张这样结构的桌子:
create table content_relations (
mainConId Integer not null,
relatedConId Integer not null,
similarity float not null,
relatedConAddedOn TIMESTAMP WITH TIME ZONE Not null);
现在我想要一个查询,通过它我应该能够在这些条件下从该表中删除行:
delete rows where count of same mainConId exceeds a max limit(say CMax),
i.e only keep CMax items per mainConId and that too with sorting according to similarity desc
i.e. keep only CMax items which are most similar in this table and remove extraneous records for every mainConId
所以在运行这个查询之后,我应该在这个表中最多有 n*CMax 行,其中 n 是唯一 mainConId 的数量。
有人可以帮我查询吗?我认为应该可以使用postgres。 感谢您的建议。
【问题讨论】:
-
@komenten 否 对于我自己的项目,我有一个表,其大小可以随 n*n 增加,所以我需要一种方法来控制表中存在的行数
-
好的。您能否附上您迄今为止尝试过的查询,以及您可能收到的任何错误消息?
-
不知道如何在查询中表达我的要求,我不是 sql 专家 :)
标签: postgresql psql delete-row