1

I have to write a query to get a result set from table T

where table T is defined as

  • Primary Key
  • column A
  • column B
  • column C

I need to get the rows that have the same value in column As and also have same value in column Cs. How to write the query? (using generic SQL query)

2 Answers 2

2

Use:

SELECT a.PrimaryKey, b.PrimaryKey
  FROM T a
 INNER JOIN T b
    ON a.columnA = b.columnA
   AND a.columnC = b.columnC
   AND a.PrimaryKey < b.PrimaryKey

This will give all couples of rows (only one time with the inequality clause).

If that is too much (having three pairs A–B, A–C, B–C) it is also possible with standard SQL to restrict to the case where the left key is the minimal key for the group (you will then get only A–B and A–C):

SELECT a.PrimaryKey, b.PrimaryKey
  FROM T a
 INNER JOIN T b
    ON b.columnA = a.columnA
   AND b.columnC = a.columnC
  LEFT JOIN T c
    ON c.columnA = a.columnA
   AND c.columnC = a.columnC
   AND c.PrimaryKey < a.PrimaryKey
 WHERE a.PrimaryKey < b.PrimaryKey
   AND c.PrimaryKey IS NULL
Sign up to request clarification or add additional context in comments.

Comments

1

To find the tuples A,C that have duplicate in table you can use

SELECT A, C, count(*)
FROM T
GROUP BY A, C
HAVING count(*) >=2

Now you can select all rows from table T that have A, C in this "duplicates" set.

Select PrimaryKey, A, B, C 
FROM T JOIN 
 (SELECT A, C, count(*)
  FROM T
  GROUP BY A, C
  HAVING count(*) >=2
 ) dupl
on T.A = dupl.A and T.C = dupl.C

1 Comment

both answers are good but I chose this one because it suits my need for current project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.