June 3, 2011 at 4:09 pm
Hi guys,
Good day, I just want to ask some help regarding the query on how to check for duplicates on a table. Here is the example:
Supposing I have TableOld which is on the SQL server then I have this FileA which has the same structure on the TableOld. I will import FileA to the TableOld and some of the records on FileA already exist on TableOld, my question is how to create a query to check if the record from FileA that was imported has a duplicate or already exist on TableOld, if it exist the query also update a certain field from the TableOld to note that it is already existing.
TableOld structure:
Name Phone Lock
Larry 1234567890
FileA structure:
Name Phone
Larry 1234567890
After the import and executing the query:
TableOld Data:
Name Phone Lock
Larry 1234567890 2
Larry 1234567890 2
Note: Data must be imported even it has duplicates, to determine that it has duplicates is the field Lock.
Thank You.
Raff:-)
June 4, 2011 at 2:35 am
It's a weird process to import duplicates on purpose and try to figure out there are duplicates afterwards... Why not add those rows to a separate table holding just the duplicate values?
But if you insist in your current process you could use "ROW_NUMBER() OVER(PARTITION BY <your column> ORDER BY <another column>) AS row" in a CTE or subquery and query for row > 1.
June 4, 2011 at 5:48 am
I'm in agreement with Lutz.
Morevover - just out of curiosity, what are you planning to do with duplicate rows after the update process is done?
delete them?
keep them there forever?
Last but not least, is this a one off process or is this a recurrent one?
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.June 4, 2011 at 2:09 pm
Are you all set on this or do you still need help?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 8, 2011 at 10:58 am
Use the merge command.
merge using
...
on
...
when matched then
...
when not matched then
...
very straightforward.
June 9, 2011 at 9:00 am
Hi, the client wants to import it even it is duplicate, the problem is on our end we have dups on a table.
Thank You.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply