datatester
datatester

Reputation: 43

SQL to retrieve only duplicated values for a certain key

I have a table in Postgres as below :

Key    Value
1234   QAB009
1234   QAB009
1234   QAB010
1235   QAB011
1236   QAB012
1236   QAB012
1236   QAB013

I want output something similar to this , only values which are repeated more than once for a certain key

Key   Value
1234  QAB009
1236  QAB012

Appreciate any help!

Upvotes: 0

Views: 429

Answers (2)

Stu
Stu

Reputation: 32614

You can use simple aggregation with a having filter:

select key, value
from t
group by key, value
having Count(*) > 1;

Upvotes: 1

Kaii
Kaii

Reputation: 20550

Simply GROUP over the criteria you consider as duplicates and use HAVING to filter for groups with more than one row.

SELECT * 
FROM tbl
GROUP BY Key, Value
HAVING COUNT(Key) > 1

Upvotes: 1

Related Questions