【问题标题】:Search for duplicates搜索重复项
【发布时间】:2018-12-04 06:57:22
【问题描述】:

我的 SQL 表中有 20 万条记录。该模型有 6 个字段。考虑到重复是 3 个或更多字段与此表中的任何其他元组的匹配。我需要为该表中的每一行查找重复项及其编号

【问题讨论】:

  • 这是对您想要做什么的非常模糊的描述。您能否添加有关架构的更多详细信息以及您想使用哪些字段来查找重复项?
  • 在我看来这实际上是一个糟糕的建模。通常,如果您以这种方式处理列,这些列实际上应该是另一个表中的行,您基本上在这里寻找“透视”数据库表。

标签: python django postgresql search duplicates


【解决方案1】:

类似这样的:

select * from (
select
 n.id as needle_id,
 h.id as haystack_id,
 case when n.a = h.a then 1 else 0 end 
 + case when n.b = h.b then 1 else 0 end 
 + case when n.c = h.c then 1 else 0 end
 + case when n.d = h.d then 1 else 0 end
 + case when n.e = h.e then 1 else 0 end
 + case when n.f = h.f then 1 else 0 end as matching_columns_count
from 
 my_table n
join 
 my_table h 
on 
 n.a = h.a 
 or n.b = h.b
 or n.c = h.c
 or n.d = h.d
 or n.e = h.e
 or n.f = h.f
order by 
 matching_columns_count desc
) z where matching_columns_count >= 3

{a,b,c,d,e,f} 是表中列的名称

我怀疑这会很快运行

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2012-05-07
    • 2015-08-28
    相关资源
    最近更新 更多