sql - PostgreSQL, return columns with invoice counts for year -
sql - PostgreSQL, return columns with invoice counts for year -
2 tables
company invoicesi want homecoming following
company name | invoice_total_2014 | invoice_total_2013 ---------------------------------------------------------- company | 2000 | 1500 company b | 1000 | 1000
so doing sort of query invoice_total columns date between function.
is possible in postgresql?
select name, sum( total * (extract(year created_at) = 2014)::integer ) invoice_total_2014, sum( total * (extract(year created_at) = 2013)::integer ) invoice_total_2013 invoice grouping name
casting boolean integer results in 0 or 1
or traditional case
select name, sum( case extract(year created_at) when 2014 total end ) invoice_total_2014, sum( case extract(year created_at) when 2013 total end ) invoice_total_2013 invoice grouping name
sql postgresql postgresql-9.1
Comments
Post a Comment