correctly plan group by queries using HAVING clause