Skip to content

Instantly share code, notes, and snippets.

@drob
Created March 19, 2014 04:59
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 drob/9635653 to your computer and use it in GitHub Desktop.
Save drob/9635653 to your computer and use it in GitHub Desktop.
SELECT remainder, array_agg(i)
FROM (
SELECT i % 2 AS remainder, i
FROM generate_series(1, 10) AS i
ORDER BY i
) nums
GROUP BY remainder;
@drewinglis
Copy link

heap=# SELECT remainder, array_agg(i)
FROM (
  SELECT i % 2 AS remainder, i
  FROM generate_series(1, 10) AS i
  ORDER BY i
) nums
GROUP BY remainder;
 remainder |  array_agg
-----------+--------------
         0 | {10,2,4,6,8}
         1 | {9,7,3,1,5}
(2 rows)

heap=# SELECT remainder, array_agg(i)
FROM (
  SELECT i % 2 AS remainder, i
  FROM generate_series(1, 10) AS i
  ORDER BY remainder, i
) nums
GROUP BY remainder;
 remainder |  array_agg
-----------+--------------
         0 | {2,4,6,8,10}
         1 | {1,3,5,7,9}
(2 rows)

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