基于AVG功能的MySQL选择

时间:2016-06-16 16:10:22

标签: mysql sql

如何从下表中选择所有国家/地区赢得超过所有国家/地区平均奖牌数的所有国家/地区:

Results Table

+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+-------------------------+
| idRESULTS | STATUS      | MEDALS | EVENTS_idEVENTS | EVENTS_ATHLETES_idATHLETES | EVENTS_ATHLETES_TEAMS_idTEAMS | EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY | EVENTS_VARIOUS_SPORTS_SPORTS_ID | awarded_medals_medal-id |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+-------------------------+
| results1  | DID-NOT-WIN | SILVER | TEN             | MS                         | RUS-WTA                       | RUS                                     | WOMENS_TENNIS                   |                       2 |
| results1  | WON         | GOLD   | TEN             | VW                         | USA-WTA                       | USA                                     | WOMENS_TENNIS                   |                       3 |
| results2  | DID-NOT-WIN | BRONZE | ATH             | JG                         | USA-TF-MEN                    | USA                                     | TRACK-AND-FIELD                 |                       3 |
| results2  | WON         | GOLD   | ATH             | UB                         | JAM-TF-MEN                    | JAM                                     | TRACK-AND-FIELD                 |                       1 |
| results3  | WON         | GOLD   | TEN-DOUBLE      | SW                         | USA-WTA                       | USA                                     | WOMENS_TENNIS                   |                       3 |
| results3  | WON         | GOLD   | TEN-DOUBLE      | VW                         | USA-WTA                       | USA                                     | WOMENS_TENNIS                   |                       1 |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+-------------------------+

我正在使用此代码:

CREATE VIEW `Countries_Athletes_who_wo_Gold` AS
select * from results where medals = 'Gold'

select EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY, medals from countries_athletes_who_wo_gold
where medals > (select AVG(medals) from countries_athletes_who_wo_gold) ;

但是没有得到所需的输出?

1 个答案:

答案 0 :(得分:1)

您首先需要编写一个计算每个国家/地区奖牌的查询。

SELECT EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY, COUNT(*) AS medal_count
FROM Countries_Athletes_who_wo_Gold
GROUP BY EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY

然后在查询中将其用作子查询两次:一次获得每个国家/地区的奖牌数,然后获得所有国家/地区的平均值。

SELECT *
FROM (
    SELECT EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY, COUNT(*) AS medal_count
    FROM Countries_Athletes_who_wo_Gold
    GROUP BY EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY
) AS by_country
JOIN (SELECT AVG(medal_count) AS avg_medals
      FROM (SELECT EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY, COUNT(*) AS medal_count
            FROM Countries_Athletes_who_wo_Gold
            GROUP BY EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY) AS by_country
      ) AS all_countries
WHERE medal_count > avg_medals

DEMO