Fix swapped group by order by SQL