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

问题描述 投票:0回答:1

可以说我有下表

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 exists来解决这个问题,但是因为我对两个不仅仅匹配1或整个记录的列感兴趣,所以我很挣扎。

sql sqlite
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.