Fixed GROUP BY issue with sorting