从方程中选择最高值,每组包含两个字段

时间:2015-12-15 13:53:40

标签: mysql

我有一个包含以下字段的国家/地区列表:

  • 旧GNP
  • 新GNP
  • 名称

我想显示一张表

| Continent | GNP Increase | Country Name 
| Europe    | 60           | United Kingdom
| Asia      | 54           | United Emerates
....

每个大陆只展示一个国家,其中该国的非洲大陆国民生产总值增幅最高。

我正在使用(((GNP / GNPOld) / GNP) * 100)计算GNP增量,我有以下mysql,但它没有得到正确的国家(它获得了每个大陆最高的GNP增长)

Select  a.Continent, 
        Max(((a.GNP - a.GNPOld) / a.GNP) * 100), 
        b.Name 
From    Country     as a 
Join    Country     as b    on  (((a.GNP - a.GNPOld) / a.GNP) * 100) = (((b.GNP - b.GNPOld) / b.GNP) * 100) 
Group By Continent;

因此,它为非洲大陆选择了最高的GNP增长,然后尝试使用相同的GNP增长(使用上述公式计算)来连接同一大洲的国家。

1 个答案:

答案 0 :(得分:1)

您可以使用sub queryjoin

执行此操作
select 
    a.Continent,
    b.GNP_Increase,
    a.Name
from Country as a
join (
    select
        Continent,
        max(((a.GNP - a.GNPOld) / a.GNP) * 100) GNP_Increase
    from Country
    GROUP BY Continent
) b
on (((a.GNP - a.GNPOld) / a.GNP) * 100) = b.GNP_Increase and
a.Continent = b.Continent