Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:19:44+00:00 2026-05-31T16:19:44+00:00

I need to write a query that will go through a table, find duplicate

  • 0

I need to write a query that will go through a table, find duplicate pairs of data, remove one of them, and then update any tables that were pointing to the removed item to point to the one I am keeping.

I have two issues with this. The first being that I am not to hot on procedural SQL, so don’t really know what structures etc would be best to accomplish this.

The second is where it gets tricky. The table holds addresses, each address has the id of the customer it belongs too. A customer can have many address. The customer must have at least one residential address. So if a customer has 2 addresses, this is rather simple, check if they are the same, if so, keep one that is a residential, remove other, update links, in this case i just need to know the above.

But what about when the customer has more then one address, how do you go about checking the pairs one by one. I know you can join the table on itself, but how do you decide which to keep, as going through all the pairs will delete all the addresses if they are all identical!!

Then there is the case of when one residential address exists, and two identical postal address, I need to remove one of the postal addresses, but still check that a residential address exists for the customer.

I realise this is a lot of corner cases, so if anyone can simply help with the structures to use to make these checks, that would be immensly helpful!

For you visual people, assume the data looks like this:

ID   CustID   Address   Type
1     25       123 St    R
2     36       567 Rd    R
3     36       567 Rd    R    < should be removed
4     36       567 Rd    P    < should be removed
5     25       99 Lane   P
6     25       99 Lane   P    < should be removed
7     25       66 Way    P
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:19:45+00:00Added an answer on May 31, 2026 at 4:19 pm

    You should be able to do something similar to what you want using two queries;

    -- Removes postal address if same as residential address.
    
    DELETE FROM Table1 WHERE Table1.ID IN (
      SELECT t1.ID FROM Table1 t1
      JOIN Table1 t2 
        ON t1.CustID=t2.CustID AND t1.Address=t2.Address
           AND t1.Type='P' AND t2.Type='R');
    
    -- Removes duplicate addresses within the same type, 
    -- keeping the one with the lowest ID.
    
    DELETE FROM Table1 WHERE Table1.ID IN (
      SELECT t1.ID FROM Table1 t1
      JOIN Table1 t2
        ON t1.CustID=t2.CustID AND t1.Address=t2.Address
           AND t1.Type = t2.Type
           AND t1.ID > t2.ID);
    

    Demo here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a SQLlite query that will delete rows from a table
I need to write a query that will group a large number of records
I need to write some sql that will allow me to query all objects
I need to write a query that will return results if all the tables
I need to write a SQL query that will generate a list of valid
I need to write an NHibernate query that will place a restriction that accesses
I need to write a query that returns all object less that or equal
I need to write a select query that joins the tables based on a
I need to write a SQL query which will get me those rows from
My users submit information for locations. I'm trying to write a query that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.