Try sorting before group by.