sqlite删除所有结果,其中a列和b列不在前n个项目中

时间:2018-09-03 09:39:55

标签: sql sqlite

让我们说一下下表

a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4
4    2    10

我想删除所有前n行在a和b中都没有与该行相同的值的所有行。

例如,各种n的结果表将是

n = 1

a    b    c
-----------
1    1    5

// No row other than the first has a 1 in a, and a 1 in b

n = 2

a    b    c
-----------
1    1    5
1    2    3
1    2    4

// The fourth row has the same values in a and b as the second, so it is not deleted. The first 2 rows of course match themselves so are not deleted

n = 3

a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4

// The fourth row has the same values in a and b as the second, so it is not deleted. The first 3 rows of course match themselves so are not deleted

n = 4

a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4 

// The first 4 rows of course match themselves so are not deleted. The fifth row does not have the same value in both a and b as any of the first 4 rows, so is deleted.

我一直在尝试使用not in或not exist方法来执行此操作,但是由于我对不仅与1或整个记录匹配的两列感兴趣,所以我一直在努力。

1 个答案:

答案 0 :(得分:1)

由于您没有定义特定的顺序,因此结果并没有完全定义,而是取决于实现的任意选择,即在limit子句中首先计算哪些行。例如,不同的SQLite版本可能会给您带来不同的结果。话虽如此,我相信您想要以下查询:

select t1.* from table1 t1, 
(select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
where t1.a=tabledist.a and t1.b=tabledist.b;

您应将N替换为所需的行数

编辑:因此,要直接从现有表中删除,您需要以下内容:

with toremove(a, b, c) as 
    (select * from table1 tt 
    EXCEPT select t1.* from table1 t1, 
    (select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
    where t1.a=tabledist.a and t1.b=tabledist.b) 
delete from table1 where exists 
(select * from toremove 
where table1.a=toremove.a and table1.b=toremove.b and table1.c=toremove.c);