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

  • Home
  • SEARCH
  • 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 840371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:36:43+00:00 2026-05-15T05:36:43+00:00

I’m using SQL Server 2008. I have a table Customers customer_number int field1 varchar

  • 0

I’m using SQL Server 2008. I have a table

Customers

customer_number int

field1 varchar

field2 varchar

field3 varchar

field4 varchar

… and a lot more columns, that don’t matter for my queries.

Column customer_number is pk. I’m trying to find duplicate values and some differences between them.

Please, help me to find all rows that have same

1) field1, field2, field3, field4

2) only 3 columns are equal and one of them isn’t (except rows from list 1)

3) only 2 columns equal and two of them aren’t (except rows from list 1 and list 2)

In the end, I’ll have 3 tables with this results and additional groupId, which will be same for a group of similar (For example, for 3 column equals, rows that have 3 same columns equal will be a separate group)

Thank you.

  • 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-15T05:36:43+00:00Added an answer on May 15, 2026 at 5:36 am

    The easiest would probably be to write a stored procedure to iterate over each group of customers with duplicates and insert the matching ones per group number respectively.

    However, I’ve thought about it and you can probably do this with a subquery. Hopefully I haven’t made it more complicated than it ought to, but this should get you what you’re looking for for the first table of duplicates (all four fields). Note that this is untested, so it might need a little tweaking.

    Basically, it gets each group of fields where there are duplicates, a group number for each, then gets all customers with those fields and assigns the same group number.

    INSERT INTO FourFieldsDuplicates(group_no, customer_no)
    SELECT Groups.group_no, custs.customer_no
    FROM (SELECT ROW_NUMBER() OVER(ORDER BY c.field1) AS group_no,
                 c.field1, c.field2, c.field3, c.field4
          FROM Customers c
          GROUP BY c.field1, c.field2, c.field3, c.field4
          HAVING COUNT(*) > 1) Groups
    INNER JOIN Customers custs ON custs.field1 = Groups.field1
                               AND custs.field2 = Groups.field2
                               AND custs.field3 = Groups.field3
                               AND custs.field4 = Groups.field4
    

    The other ones are a bit more complicated, however as you’ll need to expand out the possibilities. The three-field groups would then be:

    INSERT INTO ThreeFieldsDuplicates(group_no, customer_no)
    SELECT Groups.group_no, custs.customer_no
    FROM (SELECT ROW_NUMBER() OVER(ORDER BY GroupsInner.field1) AS group_no,
                 GroupsInner.field1, GroupsInner.field2, 
                 GroupsInner.field3, GroupsInner.field4
          FROM (SELECT c.field1, c.field2, c.field3, NULL AS field4
                FROM Customers c
                WHERE NOT EXISTS(SELECT d.customer_no
                           FROM FourFieldsDuplicates d
                           WHERE d.customer_no = c.customer_no)
                GROUP BY c.field1, c.field2, c.field3
                UNION ALL
                SELECT c.field1, c.field2, NULL AS field3, c.field4
                FROM Customers c
                WHERE NOT EXISTS(SELECT d.customer_no
                                 FROM FourFieldsDuplicates d
                                 WHERE d.customer_no = c.customer_no)
                GROUP BY c.field1, c.field2, c.field4
                UNION ALL
                SELECT c.field1, NULL AS field2, c.field3, c.field4
                FROM Customers c
                WHERE NOT EXISTS(SELECT d.customer_no
                                 FROM FourFieldsDuplicates d
                                 WHERE d.customer_no = c.customer_no)
                GROUP BY c.field1, c.field3, c.field4
                UNION ALL
                SELECT NULL AS field1, c.field2, c.field3, c.field4
                FROM Customers c
                WHERE NOT EXISTS(SELECT d.customer_no
                                 FROM FourFieldsDuplicates d
                                 WHERE d.customer_no = c.customer_no)
                GROUP BY c.field2, c.field3, c.field4) GroupsInner
          GROUP BY GroupsInner.field1, GroupsInner.field2, 
                   GroupsInner.field3, GroupsInner.field4
          HAVING COUNT(*) > 1) Groups
    INNER JOIN Customers custs ON (Groups.field1 IS NULL OR custs.field1 = Groups.field1)
                               AND (Groups.field2 IS NULL OR custs.field2 = Groups.field2)
                               AND (Groups.field3 IS NULL OR custs.field3 = Groups.field3)
                               AND (Groups.field4 IS NULL OR custs.field4 = Groups.field4)
    

    Hopefully this produces the right results and I’ll leave the last one as an exercise. 😀

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.