Fix GROUP BY query