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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:25:23+00:00 2026-05-15T12:25:23+00:00

In SQL Server, I have a table where a column A stores some data.

  • 0

In SQL Server, I have a table where a column A stores some data. This data can contain duplicates (ie. two or more rows will have the same value for the column A).

I can easily find the duplicates by doing:

select A, count(A) as CountDuplicates
from TableName
group by A having (count(A) > 1)

Now, I want to retrieve the values of other columns, let’s say B and C. Of course, those B and C values can be different even for the rows sharing the same A value, but it doesn’t matter for me. I just want any B value and any C one, the first, the last or the random one.

If I had a small table and one or two columns to retrieve, I would do something like:

select A, count(A) as CountDuplicates, (
    select top 1 child.B from TableName as child where child.A = base.A) as B
)
from TableName as base group by A having (count(A) > 1)

The problem is that I have much more rows to get, and the table is quite big, so having several children selects will have a high performance cost.

So, is there a less ugly pure SQL solution to do this?


Not sure if my question is clear enough, so I give an example based on AdventureWorks database. Let’s say I want to list available States, and for each State, get its code, a city (any city) and an address (any address). The easiest, and the most inefficient way to do it would be:

var q = from c in data.StateProvinces select new { c.StateProvinceCode, c.Addresses.First().City, c.Addresses.First().AddressLine1 };

in LINQ-to-SQL and will do two selects for each of 181 States, so 363 selects. I my case, I am searching for a way to have a maximum of 182 selects.

  • 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-15T12:25:23+00:00Added an answer on May 15, 2026 at 12:25 pm

    you can do some thing like this if you have id as primary key in your table

    select id,b,c from tablename 
    inner join
    (
    select id, count(A) as CountDuplicates
    from TableName as base group by A,id having (count(A) > 1) 
    )d on tablename.id= d.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML data stored in a varchar(max) column on SQL Server 2005.
I have some varbinary data stored in a table in MS Sql Server 2005.
i have table with filmname and actors column in sql server 2005 i want
Does each column of a table in SQL Server have a unique id? I've
I have a Money column in my SQL Server 2008 table. In my below
I have an SQL Server 2008 database with a table that has a column
I have a SQL Server 2000 with a table containing an image column. How
I have a nvarchar(50) column in a SQL Server 2000 table defined as follows:
I have a SQL Server DB table that has a column ReceivedDate defined as
I have a simple application which stores the data into the sql server database

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.