Get last 12 months data from Db with year in Postgres
I believe you wanted this:
SELECT to_char(revision_timestamp, 'YYYY-MM'), count(b.id)FROM package aJOIN package_revision b ON a.revision_id = b.revision_idWHERE revision_timestamp > date_trunc('month', CURRENT_DATE) - INTERVAL '1 year'GROUP BY 1
select count(B.id), date_part('year', revision_timestamp) as year, date_part('month',revision_timestamp) as monthfrom package as A inner join package_revision as B on A.revision_id=B.revision_id where revision_timestamp > (current_date - INTERVAL '12 months') group by date_part('year', revision_timestamp) date_part('month', revision_timestamp)
or
select count(B.id), to_char(revision_timestamp, 'YYYY-MM') as monthfrom package as A inner join package_revision as B on A.revision_id=B.revision_id where revision_timestamp > (current_date - INTERVAL '12 months') group by to_char(revision_timestamp, 'YYYY-MM')
Keep in mind that, if you filter by revision_timestamp > (current_date - INTERVAL '12 months')
, you'll get range from current date in last year (so if today is '2013-09-04'
you'll get range from '2012-09-04'
)