Skip to content

Instantly share code, notes, and snippets.

@pedro
Last active December 30, 2015 08:19
Show Gist options
  • Save pedro/7801742 to your computer and use it in GitHub Desktop.
Save pedro/7801742 to your computer and use it in GitHub Desktop.

Given a stupid table:

# SELECT * FROM sample;
 id | foo | bar 
----+-----+-----
 10 | a   | a
 11 | a   | b
 12 | a   | b
 13 | a   | c
 14 | b   | a
 15 | b   | a
(6 rows)

This is what we can do to aggregate and count how many rows have foo = bar:

# SELECT DISTINCT foo, bar, COUNT(id) OVER(PARTITION BY foo, bar) AS qty FROM sample;
 foo | bar | qty 
-----+-----+-----
 a   | a   |   1
 a   | b   |   2
 b   | a   |   2
 a   | c   |   1
(4 rows)

With that we can just filter out the rest:

# SELECT * FROM 
    (SELECT DISTINCT foo, bar, COUNT(id) OVER(PARTITION BY foo, bar) AS qty FROM sample) dups
  WHERE dups.qty > 1;
 foo | bar | qty 
-----+-----+-----
 a   | b   |   2
 b   | a   |   2
(2 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment