Skip to content

Instantly share code, notes, and snippets.

@rapimo
Created August 3, 2012 18:44
Show Gist options
  • Save rapimo/3250341 to your computer and use it in GitHub Desktop.
Save rapimo/3250341 to your computer and use it in GitHub Desktop.
count occurrences of array values in postgres using hstore
SELECT hstore(array_agg(v), array_agg(c::text)) FROM (
SELECT v, COUNT(*) as c ,1 as agg from unnest(ARRAY['foo','bar','baz','foo']) v GROUP BY v) t
GROUP BY agg
--> "bar"=>"1", "baz"=>"1", "foo"=>"2"
@wellsoliver
Copy link

YES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment