Home > Blockchain >  SQL to retrieve only duplicated values for a certain key
SQL to retrieve only duplicated values for a certain key

Time:05-11

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!

CodePudding user response:

You can use simple aggregation with a having filter:

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

CodePudding user response:

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
  • Related