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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:10:13+00:00 2026-05-18T05:10:13+00:00

I am working with a table which is an extract of a set of

  • 0

I am working with a table which is an extract of a set of other tables. All of the rows of the extract table should be unique according to keys D1, D2 and D3. They are not. It appears that an earlier developer attempted to solve this problem by using a SELECT DISTINCT across all columns being queried from this table. This will work, but only if every row which is a duplicate on (D1, D2, D3) is also a duplicate across the non-key columns (ignoring the IDENTITY column that was added to the extract table).

In other words, given rows as follows:

D1  D2  D3  C4  C5  C6
=== === === === === ===
A   B   C   X1  X2  X3
A   B   C   X1  X2  X3

then

SELECT DISTINCT D1, D2, D3, C4, C5, C6
FROM BAD_TABLE

will “work”, as there’s no difference between the rows which are duplicated on (D1,D2,D3). But if the table contained

D1  D2  D3  C4  C5  C6
=== === === === === ===
A   B   C   X1  X2  X3
A   B   C   X1  X2  X4

then SELECT DISTINCT would return two rows for the key (A,B,C). Furthermore, we would have to decide which of X3 or X4 was the “correct” value.

I know how to find the duplicates on (D1,D2,D3). I even know how to find the duplicates across all the columns (other than the IDENTITY column):

;
WITH DUPLICATES(D1,D2,D3) AS
(
    SELECT D1, D2, D3
    FROM SOURCE
    GROUP BY D1, D2, D3
    HAVING COUNT(*)>1
)
SELECT S.D1, S.D2, S.D3, S.C4, S.C5, S.C6
FROM SOURCE S
INNER JOIN DUPLICATES D
    ON S.D1 = D.D1 AND S.D2 = D.D2 AND S.D3 = D.D3
ORDER BY S.D1, S.D2, S.D3, S.C4, S.C5, S.C6

The question is, how do I find the subset of the above resultset which are duplicates on (D1,D2,D3), but not duplicates on (D1,D2,D3,C4,C5,C6)?

  • 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-18T05:10:13+00:00Added an answer on May 18, 2026 at 5:10 am

    Any reason you don’t just create another table expression to cover more fields and join to that one?

    WITH DUPLICATEKEY(D1,D2,D3) AS
    (
        SELECT D1, D2, D3
        FROM SOURCE
        GROUP BY D1, D2, D3
        HAVING COUNT(*)>1
    )
    WITH NODUPES(D1,D2,D3,C4,C5,C6) AS
    (
    SELECT 
    S.D1, S.D2, S.D3, S.C4, S.C5, S.C6
    FROM SOURCE S
    GROUP BY
     S.D1, S.D2, S.D3, S.C4, S.C5, S.C6
    HAVING COUNT(*)=1
    )
    
    SELECT S.D1, S.D2, S.D3, S.C4, S.C5, S.C6
    FROM SOURCE S
    INNER JOIN DUPLICATEKEY D
        ON S.D1 = D.D1 AND S.D2 = D.D2 AND S.D3 = D.D3
    
    INNER JOIN NODUPES D2
        ON S.D1 = D2.D1 AND S.D2 = D2.D2 AND S.D3 = D2.D3
    
    ORDER BY S.D1, S.D2, S.D3, S.C4, S.C5, S.C6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a draggable table which all working fine, though I want to be
I am working Stripes framework. I have a table which contains 100's of rows
I'm working with a huge table which has 250+ million rows. The schema is
I'm working on a large webapp, which (among other things) has a table which
While working with ActiveRecord I have a table which stores a serialized array of
I'm working on this JSF table which is used for displaying data. When I
I'm working on a trigger which needs to re-insert its data on another table.
I am working on an iPhone application which lists some items in a Table
I'm working on a project where I store an id in a table, which
I'm trying to create a database with 12 different tables which has been working

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.