I’m trying to figure out the means to do two things:
- Locate duplicate records in a table.
These are typically duplicate names in the ‘Name’ column but
specifically those where the ParentID is the same. It’s fine if I
have identical names where the ParentID is different because these
names (or Children) belong to different parents.
- Modify these duplicates.
Preferably, I would modify these duplicates by appending the ‘ID’ to the name.
I came up with a query to locate duplicates and them dump them into a temp table:
CREATE TABLE #Dup(
Name varchar(50),
CustNo varchar(7))
insert into #Dup (Name, CustNo)
SELECT [Name],[CustNo]
FROM [02Kids]
GROUP BY [Name], [CustNo]
HAVING Count(*)>1
This seems to work. When I view the data in the table I see the name and I see the ParentID identifying that indeed, this is a name that appears twice for that parent ID. Its worth noting that the name only appears once in the table. It doesn’t show two rows with the same name and ID (perhaps this is part of my problem).
Here’s the query I came up with attempting to perform the modification:
select[#Dup].[Name] + ' ' + [02Kids].[ID] as iName, [02Kids].ParentID
from #Dup
inner join [02Kids]
on #Dup.CustNo = [02Kids].ParentID
order by iName asc
Well, this sort of works, except I end up with massive amounts of duplicates. For example, one “Name” that I can confirm only has two duplicates ends up with close to 13 in total from that select query.
I may be way off here with that query (this is practice stuff I’m using to teach myself) but I’m having trouble conceiving a correct means to do this. I am still learning syntax, keywords, functions, etc so maybe there’s something I should use I just don’t know of yet.
Well to only get the matches you want in your “modification” query you’ll need to add a match on name to your join clause. Right now you are matching your duplicate record to every kid for that parent, not just the duplicates. So if one parent has 13 kids, only one of which is a duplicate, you’ll get 13 extra records.