Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to return full row where there is a Duplicate column

Status
Not open for further replies.

ppitch

Programmer
Feb 7, 2011
3
EU
Hi,

I am working on a program where I need to return every row that contains a duplicate serial number.

I have this working but it only returns the serial number and the count where as I need to return each row that the duplicate appears on.

I have been informed I need to join this query back to the original table on serial but cannot work out how to do it.

SELECT serial,
COUNT(serial) AS NumOccurrences
FROM address
GROUP BY serial
HAVING ( COUNT(serial) > 1 )


Any help would be greatly appreciated.

 
Code:
SELECT address.*
FROM address
INNER JOIN (SELECT Serial
            FROM address
            GROUP BY Serial
            HAVING (COUNT(serial) > 1 )) Tbl1
      ON Address.Serial = Tbl1.Serial


Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Thanks Borislav,

I have tried this but it seems to return the whole table and not rows with duplicate serial numbers.

I will try modifiying this and see where I get.

 
In SQL 2005+ you can also do
Code:
;with cte as (select *, count(*) over (partition by Serial) as NumOccurrences from Address)

select * from cte where NumOccurrences >=2

PluralSight Learning Library
 
Thanks

I will give this a go and hopefully you have fixed my issue.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top