SELECT SQL_CACHE a.*, b.id_match_schedule, count(e.id_match_points) AS count_match, sum(e.count_match_points) AS count_match_points, count(d.id_match_result) AS count_match_v, count(c.id_match_result) AS count_match_vb, count(h.id_match_result) AS count_match_h, count(pb.id_match_result) AS count_match_pb, count(p.id_match_result) AS count_match_p, sum(s.count_match_score) AS count_match_score_1, sum(f.count_match_score) AS count_match_score_2, z.name_command FROM structure_command AS a LEFT JOIN match_schedule AS b ON (b.season_match_schedule = a.id_season AND b.structure_match_schedule = a.id_structure AND (b.command1_match_schedule=a.id_command OR command2_match_schedule=a.id_command) AND b.stage_match_schedule IN (13)) LEFT JOIN match_points AS e ON (e.id_match_schedule = b.id_match_schedule AND e.id_command = a.id_command) LEFT JOIN match_result AS d ON (d.id_match_schedule = b.id_match_schedule AND d.id_command=a.id_command AND d.status_match_result='В') LEFT JOIN match_result AS c ON (c.id_match_schedule = b.id_match_schedule AND c.id_command=a.id_command AND c.status_match_result='ВБ') LEFT JOIN match_result AS h ON (h.id_match_schedule = b.id_match_schedule AND h.id_command=a.id_command AND h.status_match_result='Н') LEFT JOIN match_result AS pb ON (pb.id_match_schedule = b.id_match_schedule AND pb.id_command=a.id_command AND pb.status_match_result='ПБ') LEFT JOIN match_result AS p ON (p.id_match_schedule = b.id_match_schedule AND p.id_command=a.id_command AND p.status_match_result='П') LEFT JOIN match_score AS s ON (s.id_match_schedule = b.id_match_schedule AND s.technical_match_score < 1 AND (b.command1_match_schedule = a.id_command OR b.command2_match_schedule = a.id_command) AND s.id_command = a.id_command) LEFT JOIN match_score AS f ON (f.id_match_schedule = b.id_match_schedule AND f.technical_match_score < 1 AND (b.command1_match_schedule = a.id_command OR b.command2_match_schedule = a.id_command) AND f.id_command != a.id_command) LEFT JOIN command AS z ON z.id_command = a.id_command LEFT JOIN tournament_table AS ts ON (ts.id_season = a.id_season AND ts.id_structure = a.id_structure AND ts.id_conference = a.id_conference AND ts.id_stage='13' AND ts.id_command = a.id_command) WHERE a.id_season = '3' AND a.id_structure = '9' AND a.out_competition < 1 AND ((b.command1_match_schedule NOT IN (SELECT id_command FROM structure_command WHERE id_stage = '3' AND id_structure = '9' AND id_conference = '' AND out_competition > 0)) AND (b.command2_match_schedule NOT IN (SELECT id_command FROM structure_command WHERE id_season = '3' AND id_structure = '9' AND out_competition > 0))) AND a.id_structure_command IN (SELECT id_structure_command FROM structure_command_stage WHERE id_stage = '13') GROUP BY a.id_command ORDER BY sum(e.count_match_points) DESC, ts.sort_command, (sum(s.count_match_score) - sum(f.count_match_score)) DESC, count(d.id_match_result) DESC, count(h.id_match_result) DESC, count(c.id_match_result) DESC, count(e.id_match_points), z.name_command