Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Find duplicate records

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2014-07-28 : 14:35:33
How can I pull out duplicate records based on certain fields?

Table called Bank

I want to pull out records that have duplicate inv_no, cus_no, amount,ordernum

Not all the fields are the same in each record. But I want the records that have these fields that are the same.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-07-28 : 14:40:32
select inv_no, cus_no, amount, ordernum, count(*)
from Bank
group by inv_no, cus_no, amount, ordernum
having count(*) > 1

Add a unique constraint to these to prevent this in the future (if this condition is not supposed to happen).

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -