explain analyze select sum(total_cost)as cost,date_trunc('month',analytics_date)as monthDate from tableA where inr_id in(select id from tableB where ct_id ='4028cb972f1ff337012f1ffa1fee0001') and analytics_date between '2013-01-14 00:00:00' and '2013-05-29 00:00:00' group by monthDate order by 2 desc