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 6878953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:47:12+00:00 2026-05-27T04:47:12+00:00

I have a SQL table containing customer information (we’ll call it CustomerDB ), including

  • 0

I have a SQL table containing customer information (we’ll call it CustomerDB), including an address column. Many of the rows in this table have exact duplicate addresses. The business requirement is to retrieve rows from the database such that:

  • If there are more then three exact duplicates, only take three (ignore the rest)
  • Which three is not important; any three will do (it does not have to be the "first three").

So, if I have a data set like this (I have also entered this in the Data Explorer, to make it easy to test queries against):

ID     NAME       COMPANY    ADDR1
16242  TOM E      Company A  101 First RD  
16241  RONALD J   Company B  12 Tenth AVE  
16235  KENNETH H             12 Tenth AVE  
16238  MICHAEL H  Company C  12 Tenth AVE  
16243  ANTONIO D  Company D  264 Long ST STE 5  
16237  MICHAEL B  Company E  264 Long ST STE 5  
16234  WALTER L   Company F  73 North RD  
16236  CARL O     Company G  73 North RD  
16239  MICHAEL S  Company H  73 North RD  
16240  MICHAEL I  Company I  73 North RD 

I would like to get all of those rows except for one of the "73 North RD" records. I hope I’m making some sense here.

My mind does not work very well in set-based operations, so I’m confused about how to do this. I would prefer a solution with an explanation of why it works. Since I’d like to be "taught how to fish", so to speak =)

Additional Information:

  • ID is an int primary key (auto-incremented)
  • All other columns are text.
  • Sometimes I get these datasets as Access DBs, sometimes they are in SQL Server. So, I’d prefer a solution that works in both (i.e. does not use CROSS APPLY or CTE‘s)
  • 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-27T04:47:13+00:00Added an answer on May 27, 2026 at 4:47 am

    A possible solution could be

    SELECT c1.id, c1.name, c1.company, c1.addr1
    FROM CustomerDB c1 LEFT JOIN CustomerDB c2
        ON (c1.addr1 = c2.addr1 and c1.id >= c2.id)
    GROUP BY c1.addr1, c1.id, c1.name, c1.company
    HAVING COUNT(*) <= 3​
    

    The magic (nice definition you gave) is done by GROUP BY ... HAVING ... part.
    Sure you know WHERE is used to filter data while selecting; well, HAVING is used in conjunction with GROUP BY to filter grouped data (so after grouping).
    So I first group data (order is important) by address and then limit the count for each group to three.
    Now let’s talk about the inner part (you didn’t ask this, but I thought it was good to provide you a complete example).
    The ON part is used to tell the engine which fields are to be used to join tables; here I match tables on address and then I take records having an higher id (so, sorting ascending).

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

Sidebar

Related Questions

We have a SQL Server table containing Company Name, Address, and Contact name (among
I have a SQL Server 2000 with a table containing an image column. How
Assuming I have a SQL Server 2005 table with an xml column containing the
I have a SQL Server table containing a TEXT column and a DATETIME column
I have a SQL table containing train schedules. The table looks something like this:
I have a SQL table with indexes containing leads to call. About 30 users
I have a table containing hierarchical data. There are currently ~8 levels in this
I have table containing information about a series of x number of quizzes. It
I have a table containing persons information (one row per person) and another table
I have a MySQL table containing phone numbers that may be formatted in many

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.