Skip to content

Instantly share code, notes, and snippets.

@mgadzhi
Last active December 18, 2015 19:09
Show Gist options
  • Save mgadzhi/5831111 to your computer and use it in GitHub Desktop.
Save mgadzhi/5831111 to your computer and use it in GitHub Desktop.
Grouping by month and year in Postgres
SELECT CAST(date_part('year', date) AS varchar(4)) || '-' || CAST(date_part('month', date) AS varchar(2)) AS month, SUM(amount)
FROM withdrawals
WHERE some_predicate()
GROUP BY month
ORDER BY month;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment