Mysql 每个条件只提供一个(最高排名)的查询

Mysql query providing only one (highest rank) per condition

我得到了一个数据库 table,其中包含以下数据:

id | country | date       | rank   | sport
--------------------------------------------
1  | UK      | 2000-01-30 | 1      | tennis
2  | US      | 2000-01-30 | 2      | tennis
3  | UK      | 2000-01-30 | 3      | tennis
4  | FR      | 2000-01-30 | 4      | tennis
5  | UK      | 2000-01-30 | 5      | tennis
6  | NL      | 2000-01-30 | 6      | tennis
7  | DE      | 2000-01-30 | 1      | golf
8  | ES      | 2000-01-30 | 2      | golf
9  | GR      | 2000-01-30 | 3      | golf
10 | GR      | 2000-01-30 | 4      | golf
11 | ES      | 2000-01-30 | 5      | golf
12 | NL      | 2000-01-30 | 6      | golf
13 | US      | 2000-01-31 | 1      | tennis
14 | FR      | 2000-01-31 | 2      | tennis
15 | UK      | 2000-01-31 | 3      | tennis
16 | UK      | 2000-01-31 | 4      | tennis
17 | FR      | 2000-01-31 | 5      | tennis
18 | BE      | 2000-01-31 | 6      | tennis
19 | DE      | 2000-01-31 | 1      | golf
20 | BE      | 2000-01-31 | 2      | golf
21 | ES      | 2000-01-31 | 3      | golf
22 | US      | 2000-01-31 | 4      | golf
23 | UK      | 2000-01-31 | 5      | golf
24 | NL      | 2000-01-31 | 6      | golf

等等

期望输出

我希望看到所选国家/地区的以下输出:

因此,例如,英国的结果应如下所示:

UK:

date         |  avg. rank | amount sports in top 6 rank |   sum of highest rank per sport
--------------------------------------------------------------------------------------------
2000-01-30   |  1,0       | 1                           |   1                            
2000-01-31   |  4,0       | 2                           |   8                            

看起来并不太难,但是我没能成功。

尝试过的解决方案

我使用以下 mysql 查询:

SELECT
        date,
        AVG(rank) as avgRank,
        COUNT(rank) as numRank,
        SUM(rank) as sumRank
    FROM `table`
    WHERE
        country='UK'
    GROUP BY date
    ORDER BY
        date,rank ASC

它几乎可以工作,但是,我有一个小问题。输出如下:

UK:

date         |  avg. rank (avgRank) | amount sports in top 6 rank (numRank) |   sum of highest rank per sport (sumRank)
--------------------------------------------------------------------------------------------
2000-01-30   |  4,5                 | 3                                     |   9                            
2000-01-31   |  4,0                 | 3                                     |   12   

所以,它似乎计算了英国的所有符号,而不是每项运动每天的最高排名。 如何更改查询以使其正常工作?我试图将 "sport" 添加到 GROUP BY 语句中,但这给我留下了以下内容(每项运动一行):

UK:

date         |  avg. rank | amount sports in top 6 rank |   sum of highest rank per sport
--------------------------------------------------------------------------------------------
2000-01-30   |  4,5       | 3                           |   9                                        
2000-01-31   |  3,5       | 2                           |   7   
2000-01-31   |  5,0       | 1                           |   5

谁能帮帮我? :)

我想这个查询会给你想要的结果。它使用子查询来查找每个国家和运动每天的最高排名,然后聚合子查询之外的那些:

SELECT t1.date,
       t1.country,
       AVG(min_rank) AS `avg rank`,
       SUM(min_rank <= 6) AS `num top 6`,
       COUNT(sport) AS `num sports`
FROM (SELECT `date`, `country`, `sport`,
              MIN(`rank`) AS min_rank
      FROM `table`
      GROUP BY `date`, `country`, `sport`) t1
WHERE t1.country = 'UK'
GROUP BY t1.date, t1.country

输出

date        country     avg rank    num top 6   num sports
2000-01-30  UK          1           1           1
2000-01-31  UK          4           2           2

Demo on dbfiddle