Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created October 4, 2012 16:37
Show Gist options
  • Save robertsosinski/3834827 to your computer and use it in GitHub Desktop.
Save robertsosinski/3834827 to your computer and use it in GitHub Desktop.
Example crosstab function
SELECT c.id, c.name, c.jan, c.feb, c.mar
FROM crosstab(
'select buyers.id, buyers.name, transactions.month, sum(transactions.amount) from transactions inner join buyers on buyers.id = transactions.buyer_id group by buyers.id, buyers.name, transactions.month order by buyers.id, transactions.month',
'select generate_series(1, 3)'
) c (id integer, name character varying, jan numeric(12,2), feb numeric(12,2), mar numeric(12,2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment