Skip to content

Instantly share code, notes, and snippets.

@todgru
Created May 3, 2022 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/128c684721e0cf3c27c03fb49938175b to your computer and use it in GitHub Desktop.
Save todgru/128c684721e0cf3c27c03fb49938175b to your computer and use it in GitHub Desktop.
Collect rows by column and group by having count greater than. postgresql sql

Group the rows in the userLogins table by the userId column where the userName matches foo bar. If the count of those groups are greater than one, show the user id and count.

SELECT 
  "userId", count("userId")
FROM 
  "userLogins"
WHERE 
  "userName" = 'foo bar'
GROUP BY 
  "userId"
HAVING 
  count("userId") > 1 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment