多列去重的 SQL 查询

3

我有这些数据,我想找出在字段1、2、3、4中有重复数据但ID不同的情况。

id    field1 field2 field3 field4    
====  ====== ====== ===== =======    
1       A      B     C      D    
2       A      B     C      D    
3       A      A     C      B   
4       A      A     C      B

所以,在任何可能的情况下,在这种情况下,我希望它以某种方式向我显示: 1和2是重复的 3和4是重复的。
3个回答

4

不要使用 SELECT DISTINCT,而是选择字段和行数的计数。使用 HAVING 过滤掉超过一行的项目,例如:

select field1
      ,field2
      ,field3
      ,field4
      ,count (*)
  from foo
 group by field1
         ,field2
         ,field3
         ,field4
having count (*) > 1

然后,您可以将原始表与查询结果重新连接。


应该使用更有效的 count(id),而不是其他方式! - user57508

4

One way to do this is to use having and group by


esben=# select * from test;
 id | a | b | c | d
----+---+---+---+---
  1 | 1 | 2 | 3 | 4
  2 | 1 | 2 | 3 | 4
  3 | 1 | 1 | 3 | 2
  4 | 1 | 1 | 3 | 2
(4 rows)

esben=# select count(id),a,b,c,d from test group by a,b,c,d having count(id) >1;
 count | a | b | c | d
-------+---+---+---+---
     2 | 1 | 2 | 3 | 4
     2 | 1 | 1 | 3 | 2
(2 rows)

This doesn't list the actual id's though, but without the actual output you want it is hard to tell you how to get about that.


1
SELECT * 
FROM [TableName]
WHERE ID IN(SELECT MIN(ID) 
            FROM [TableName] 
            GROUP BY CONCAT(field1, field2, field3, field4))

这将返回id为1和3的完整行


concat函数用于连接字符串值。CONCAT('AA','B','C','D')等同于CONCAT('A','AB','C','D'),尽管这不是重复数据。"SELECT * FROM [TableName] WHERE ID IN( SELECT MIN(ID) FROM [TableName] GROUP BY field1, field2, field3, field4)"将给出每个唯一数据集的最小ID。 - Renze de Waal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接