How do I query only rows where multiple instances with the same ID exist with Postgres?
1
vote
1
answer
7392
views
I have a table where I want to gather some statistics, but only for items that are repeated two or more times. Here is how the a simplified table looks like:
CREATE TABLE test (
id bigint,
something text
);
Most rows have a unique
id
, some have repeated entries where something
differs.
What I'm wondering is whether there is a way to read the table but only items that have 2 or more rows with the same id
.
I know how to count the number of duplicated rows using the COUNT()
and GROUP BY
:
SELECT id, COUNT(id) AS count FROM test GROUP BY id;
I don't really see how to use the COUNT(id)
in a WHERE
or FILTER
clause (since aggregate functions are not allowed there).
---
Just in case, a very small data sample would be:
id | something
----|-----------
1 | first
2 | second
2 | third
In the result I want to see:
id | something
----|-----------
2 | second
2 | third
Asked by Alexis Wilke
(135 rep)
Feb 2, 2023, 07:58 PM
Last activity: Aug 5, 2024, 11:23 PM
Last activity: Aug 5, 2024, 11:23 PM