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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 issue with duplicates

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-10-21 : 13:44:04
This is kind of a simple one...
How do I get the duplicate records in a table

eg:
tbl_emp_details

lets say the columns are emp_id,emp_name,emp_age,emp_address,date_assigned

Now 2 queries i need to do is
1. get only the duplicate records
2.get the distinct records with max date

Kristen
Test

22859 Posts

Posted - 2004-10-21 : 13:52:11
SELECT emp_id,emp_name,emp_age,emp_address,date_assigned
FROM tbl_emp_details
GROUP BY emp_id,emp_name,emp_age,emp_address,date_assigned
HAVING COUNT(*) > 1

SELECT emp_id,emp_name,emp_age,emp_address,MAX(date_assigned_
FROM tbl_emp_details
GROUP BY emp_id,emp_name,emp_age,emp_address

Kristen

Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-10-21 : 14:18:45
Thanks a lot...Here both queries are fine . a small difference I was looking for
Here lets say in the first query lets say the only difference is emp_id and the rest of the fields are same then Iam seeing only one record for that and I want to see both duplicate records
Go to Top of Page

jmangione
Starting Member

7 Posts

Posted - 2004-10-21 : 14:27:45
If I understand you correctly you could try:

SELECT max(emp_id),emp_name,emp_age,emp_address,date_assigned
FROM tbl_emp_details
GROUP BY emp_name,emp_age,emp_address,date_assigned

You need to decide what emp_id you'd like to return (max or Min) but that will give you the "unique" data set beyond the identity.
Hope this helps!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-10-21 : 14:41:59
Just leave the emp_id column out of the SELECT and GROUP BY statements

Basically you will get a list of duplicates for the columns which are iin the GROUP BY statement (makes sense to also include them in the SELECT statement, but you don't have to ...)

Kristen
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-10-21 : 15:09:06
Hi kristen,
But they want to view the emp_id as well
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-10-22 : 02:12:37
[code]
SELECT emp_id, emp_name, emp_age, emp_address, date_assigned
FROM tbl_emp_details E
JOIN
(
SELECT emp_name, emp_age, emp_address, date_assigned
FROM tbl_emp_details
GROUP BY emp_name, emp_age, emp_address, date_assigned
HAVING COUNT(*) > 1
) T
ON T.emp_name = E.emp_name
AND T.emp_age = E.emp_age
AND T.emp_address = E.emp_address
AND T.date_assigned = E.date_assigned
ORDER BY emp_name, emp_age, emp_address, date_assigned
[/code]
Kristen
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-10-22 : 09:07:17
Does this help at all?

http://weblogs.sqlteam.com/jeffs/archive/2004/10/07/2190.aspx

- Jeff
Go to Top of Page
   

- Advertisement -