Add necessary group by clauses when using aggregate functions.