如何(通常)计算重复行(并删除重复项)?

时间:2013-09-08 04:50:14

标签: sql sqlite duplicate-removal

是否有通用的SELECT语句来检测重复的行(“相同”,所有列都相同)? E.G,第2栏和第2栏下表中的4

 titel                             | interpret        | jahr
-----------------------------------+------------------+-----
 Beauty                            | Ryuichi Sakamoto | 1990
 Goodbye Country (Hello Nightclub) | Groove Armada    | 2001
 Glee                              | Bran Van 3000    | 1997
 Goodbye Country (Hello  Nightclub)| Groove Armada    | 2001

或者我需要一张特定于表的SELECT吗?

有人给了我一个带有多个表的Sqlite d / b,每个表看起来好像有多个相同的行(每个表中有不同的列),所以我更喜欢通用的解决方案。

之后,我必须弄清楚如何删除重复项。也许我可以在SELECT上使用DISTINCT,存储在临时表中,删除原始文件并重命名临时表?

1 个答案:

答案 0 :(得分:3)

你一直都有正确的想法。您必须在每个表上运行以下内容:

select distinct titel, interpret, jahr from table1

您可以将不同的行转储到另一个表中,如下所示:

create table table2 as
select distinct titel, interpret, jahr from table1

然后您可以删除初始表:

drop table table1

将新创建的表重命名为table1,如下所示:

alter table table2 rename to table1

要查找表中每条记录的行号:

select a.rowid, a.* from table1 a

仅查找重复记录的行号:

select a.rowid, a.* 
from table1 a 
inner join 
(
      select titel, interpret, jahr 
      from table1 
      group by titel, interpret, jahr 
      having count(*) > 1
) b on 
      a.titel = b.titel 
      and a.interpret = b.interpret 
      and a.jahr = b.jahr
相关问题