SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query for finding Duplicates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mchandrav
Starting Member

USA
7 Posts

Posted - 11/30/2012 :  12:41:01  Show Profile  Reply with Quote
Hi All

I have a table with columns Name, Type, ColumnnC1, ColumnnC2, ColumnnC3, ColumnnC4, ColumnnC5. The unique record is identified based on the combination of Name and Type. Now, I need to retrieve duplicate records from this table.
When I run the following query, I get around 200 records which looks okay.

SELECT NAME, COUNT(NAME) AS Total
FROM TABLE
WHERE Type = 'Value1'
GROUP BY NAME
HAVING COUNT(NAME) > 1

But the results count from this query do not match with the above query

SELECT NAME, Type, ColumnnC1, ColumnnC2, ColumnnC3, ColumnnC4, ColumnnC5, COUNT(NAME) AS Total
FROM TABLE
WHERE Type = 'Value1'
GROUP BY NAME, Type, ColumnnC1, ColumnnC2, ColumnnC3, ColumnnC4, ColumnnC5
HAVING COUNT(NAME) > 1

Can someone please help with the second query.

Thanks in advance
M

Edited by - mchandrav on 11/30/2012 12:47:11

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 11/30/2012 :  12:55:14  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Think you want
SELECT NAME, Type, ColumnnC1, ColumnnC2, ColumnnC3, ColumnnC4, ColumnnC5, COUNT(NAME) AS Total
FROM TABLE
WHERE Type = 'Value1'
and name in (select name from table where type = 'value1' group by name having count(*)> 1)
GROUP BY NAME, Type, ColumnnC1, ColumnnC2, ColumnnC3, ColumnnC4, ColumnnC5

The having count(name) > 1 doesn't work because the group is distinct over the group by columns not just name.

as name, type are meant to be unique you might want
SELECT NAME, Type, max(ColumnnC1), max(ColumnnC2), max(ColumnnC3), max(ColumnnC4), max(ColumnnC5), COUNT(NAME) AS Total
FROM TABLE
WHERE Type = 'Value1'
GROUP BY NAME, Type
having count(*) > 1




==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/02/2012 :  02:02:04  Show Profile  Reply with Quote

SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY NAME,Type ORDER BY NAME) AS Seq,*
FROM Table
)t
WHERE Seq > 1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000