Your question leaves room for interpretation. This test test shows two nested steps:
CREATE TEMP TABLE x (ad_id int, distance int); INSERT INTO x VALUES (510, 0),(956, 3),(823, 3),(880, 2) ,(523, 3),(467, 0),(843, 1),(816, 9) ,(533, 4),(721, 7),(288, 3),(900, 3) ,(526, 9),(750, 7),(302, 8),(463, 6) ,(742, 8),(804, 2),(62, 7),(880, 2) ,(523, 3),(467, 0),(843, 1),(816, 9) ,(533, 4),(721, 7),(288, 3),(900, 3) ,(526, 9),(750, 7),(302, 8),(816, 9) ,(533, 4),(721, 7),(288, 3),(900, 3) ,(533, 4),(721, 7),(288, 3),(396, 5);
How many duplicates per value?
SELECT ad_id, count(*) AS ct FROM x GROUP BY 1;
Result:
ad_id | ct -------+---- 62 | 1 288 | 4 302 | 2 396 | 1 ...
Read: ad_id 62 exists 1x, ad_id 288 exists 4 times, ...
"How can I calculate how many times a row has a duplicate?"
SELECT ct ,COUNT (*) AS ct_ct FROM (SELECT ad_id, COUNT (*) AS ct FROM x GROUP BY 1) a GROUP BY 1 ORDER BY 1;
Result:
ct | ct_ct ----+--- 1 | 8 2 | 7 3 | 2 4 | 3
Reading: 8 occurrences of " ad_id unique", 7 occurrences of "2 lines with the same ad_id ", ...