如何返回唯一值并找到重复值? SQL

时间:2013-07-19 15:34:32

标签: sql sql-server tsql sql-server-2005

我有这个一般的想法,找到从这篇文章中取出的重复值: Select statement to find duplicates on certain fields

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

这非常适合查找重复项,但我还需要提取一个唯一的数字,在这种情况下是一个“订单号”列,它与返回的每一行一起。这个唯一值不能在上面的方法中使用,因为那样就不会返回任何行,因为没有行会完全重复。我需要能够返回此数据,但还要查找在表中多次出现的记录。我认为这可以通过联合或使用exists来完成,但不确定如何实现。有什么想法吗?

样本结果的想法:

order number, field1, field2, field3
123             a       b        c
456             d       e        f
789             a       b        c

希望它像这样返回订单号123和789:

order number, field1, field2, field3
123             a       b        c
789             a       b        c   

2 个答案:

答案 0 :(得分:3)

;with a as
(
select count(*) over (partition by field1,field2,field3) count, order_number, field1,field2,field3
from table_name
)
select order_number, field1,field2,field3 from a where count > 1

答案 1 :(得分:1)

我不完全确定这是否是你想要的,但听起来似乎也许?

select min(t2.order_no), t2.field1, t2.field2, t2.field3, t1.cnt
from table_name t2, (
    select field1,field2,field3, count(*)
      from table_name
      group by field1,field2,field3
      having count(*) > 1
      ) t1
where t1.field1 = t2.field1      
and t1.field2 = t2.field2
and t1.field3 = t2.field3
group by t2.field1, t2.field2, t2.field3, t1.cnt

对于重复数据删除子查询中返回的每条记录,外部查询将向该记录附加与给定字段组合匹配的最小“订单号”。如果这不是您想要的,请澄清。一些样本数据和样本输出会有所帮助。

编辑:从您发布的示例数据中,您似乎只想返回有重复记录的记录。如果这就是你要找的东西,试试这个:

select * 
from  table_name t2
where exists (
    select field1,field2,field3, count(*)
      from table_name t1
      where t1.field1 = t2.field1      
      and t1.field2 = t2.field2
      and t1.field3 = t2.field3 
      group by field1,field2,field3
      having count(*) > 1
      )

SQLFiddle