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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:22:10+00:00 2026-06-17T14:22:10+00:00

Okay, I have a table with some junk data and no unique identifier column.

  • 0

Okay, I have a table with some junk data and no unique identifier column. Let me give you an example of the table I’m working with:

     A    |   B   |  C   |        D         |   E  |
  --------------------------------------------------
1.  Fiona | Smith | NULL | 2152 Cherry Lane | CA   |
2.  Fiona | Smith | NULL | NULL             | NULL |
3.  Bill  | NULL  | ACME | 2903 Center Road | WA   |
4.  Bill  | NULL  | ACME | NULL             | NULL |
5.  NULL  | NULL  | ABC  | 2300 Water St    | PA   |
6.  NULL  | NULL  | ABC  | 2300 Water St    | PA   |
7.  NULL  | NULL  | NULL | 3455 B Street    | CO   |

I need to write a SELECT statement that grabs only distinct rows. For example, take rows 1 and 2. They both obviously refer to the same person, but they’re only partially duplicate. Out of those two, I want row 1 included in my SELECT statement because it contains the most data in each column. Same goes for rows 3 and 4. Row 3 is the one I want included. For rows 5 and 6, it does not matter which one is selected since they both are exact duplicates. Row 7 would be included by default since it is distinct (meaning A, B and C, not just A and B).

Here’s what I have tried:

SELECT A, B, C = MAX(D), MAX(E), 
FROM dbo.Data
GROUP BY A, B, C;

This seems to grab the unique rows I want, but the data is somehow placed into the wrong columns.

  • 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-06-17T14:22:11+00:00Added an answer on June 17, 2026 at 2:22 pm

    This approach treats D and E as equal:

    DECLARE @x TABLE
    (
      A VARCHAR(32), 
      B VARCHAR(32), 
      C VARCHAR(32), 
      D VARCHAR(32), 
      E VARCHAR(32)
    );
    
    INSERT @x VALUES
    ('Fiona', 'Smith', NULL,   '2152 Cherry Lane',  'CA'),
    ('Fiona', 'Smith', NULL,   NULL,                NULL),
    ('Bill',  NULL,    'ACME', '2903 Center Road',  'WA'),
    ('Bill',  NULL,    'ACME', NULL,                NULL),
    (NULL  ,  NULL,    'ABC',  '2300 Water St',     'PA'),
    (NULL  ,  NULL,    'ABC',  '2300 Water St',     'PA'),
    (NULL  ,  NULL,    NULL,   '3455 B Street',     'CO'),
    ('Bob',   'Barker',NULL,   NULL,                NULL),
    ('Bob',   'Barker',NULL,   NULL,                'NY');
    
    ;WITH x AS
    (
      SELECT A,B,C,D,E, rn = ROW_NUMBER() OVER 
      (
        PARTITION BY A,B,C
        ORDER BY COALESCE(LEN(LEFT(D,1)),0) + COALESCE(LEN(LEFT(E,1)),0) DESC
      )
      FROM @x
    )
    SELECT A,B,C,D,E
    FROM x WHERE rn = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

okay I have some table like this.. table TopicForum ------------------------- - idforum (PK) -
Okay, so I have a program which outputs some specific data to a tab
Okay, so I have a (SQLite) table called log wherein one column (time) is
I am wondering if it is okay to have master and detail table for
Okay, here's the situation: We have a table of about 50 columns (created by
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay, I'm stumped here. I have a control that is comprised of a table.
For example I have the following tables resulting from: CREATE TABLE A (Id int,
Okay, I need some solid OOP advice. Here's the setup: I have a player
Yesterday I was working on a shell script to perform some moderately complex table

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.