fixed an error in an SQL statement

needed to fix the group by and order by clauses