/* try to avoid the nesting SELECT reportdate, maturity, isin, count(issuepriceid) filter (where abs(quantity) < 5000000) as TotalOddlotTrades, count(issuepriceid) filter (where abs(quantity) >= 5000000) as TotalBlockTrades, count(issuepriceid) as TotalTrades FROM mka.trace_history where reportdate >= '2013/06/01' and reportdate < '2013/12/31') rep and rep.oasspread <= 400 and rep.isin in ('US74251VAD47', 'US125896BA74', 'US15189TAQ04', 'US52107QAE52', 'US441060AJ93', 'US032511BH96', 'US40414LAA70', 'US438516AZ99', 'US441060AL40', 'US00287YAB56', 'USU0029QAB24', 'US74251VAG77', 'US00287YAJ82', 'US441060AM23', 'US441060AN06', 'US609207AA31', 'US55279HAD26', 'US92343VCE20', 'US89620JAB70', 'USU21008AA25', 'USU8603MAB82', 'US865033AB04', 'US210382AA50', 'US87264LAB71', 'US39248TAA43', 'US464592AN47') and rep.subtype = 'CORP' group by reportdate, maturity, isin order by reportdate, maturity, isin /* EXPLAIN "GroupAggregate (cost=3737573.12..3737852.91 rows=7458 width=44)" " Group Key: trace_history.reportdate, trace_history.maturity, trace_history.isin" " -> Sort (cost=3737573.12..3737591.78 rows=7462 width=32)" " Sort Key: trace_history.reportdate, trace_history.maturity, trace_history.isin" " -> Bitmap Heap Scan on trace_history (cost=198146.70..3737093.12 rows=7462 width=32)" " Recheck Cond: ((reportdate >= '2013-06-01'::date) AND (reportdate < '2013-12-31'::date))" " Filter: ((oasspread <= '400'::double precision) AND ((subtype)::text = 'CORP'::text) AND ((isin)::text = ANY ('{US74251VAD47,US125896BA74,US15189TAQ04,US52107QAE52,US441060AJ93,US032511BH96,US40414LAA70,US438516AZ99,US441060AL40,US00287YAB56, (...)" " -> Bitmap Index Scan on reportdate_mat_isin_idx (cost=0.00..198144.84 rows=6617227 width=0)" " Index Cond: ((reportdate >= '2013-06-01'::date) AND (reportdate < '2013-12-31'::date))" */