我有一张表格如下:
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 3
4 | 5 | 6
4 | 5 | 6
4 | 5 | 6
4 | 5 | 6
7 | 8 | 9
7 | 8 | 9
7 | 8 | 9
我想删除重复内容,只保留一个值超过3次的记录值。应该是这样的:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
7 | 8 | 9
7 | 8 | 9
我有以下代码:
DELETE FROM test5 X
WHERE x.ROWID > ANY
(SELECT Y.ROWID
FROM test5 Y
WHERE
X.A = Y.A
AND
X.B = Y.B
AND
X.C = Y.C
);
如何只保留前两个元组(123和456)的一个副本并保留第三元组的所有副本?
答案 0 :(得分:0)
如果我清楚地理解您的问题,为什么不使用子查询将删除限制为具有所需值的行。像这样:
null
这应该只删除前两个元组的重复项。
答案 1 :(得分:0)
以下语句删除表中发生三次以上的所有行(最旧的行除外)。
delete test5 where rowid in (
with data as(
select a,b,c, min(rowid) keep_rowid
from test5
group by a,b,c
having count(*) > 3)
select t.rowid from test5 t, data d
where t.a = d.a
and t.b = d.b
and t.c = d.c
and t.rowid <> d.keep_rowid);
答案 2 :(得分:0)
你可以试试这个:
delete test5
where rowid not in (select rowss
from (select A, count(*) counts, min(rowid) rowss
from test5
group by A
having count(*) > 3))--keep one row which's count bigger then 3 (like 1|2|3,4|5|6)
and A not in(select A from test5 group by A having count(*) <= 3)--keep rows which's counts smaller and equal then 3 (like 7|8|9)
表和列名称应该是抱歉的。