Fixing query and correcting some $group to $table.