1

作为 Mondial DB 的一部分,我正在尝试回答以下问题。

-- For each country display the city with the highest population together with the number of the population.

这些表是:

国家(代码、名称、首都、省、地区、人口)

城市(名称,国家,省,经度,纬度,人口)

到目前为止,我有以下内容:

SELECT 
    Country.Name, MaxCity.CityName, MaxCity.Population
FROM
    (SELECT 
        MAX(Population) AS Population,
            City.Country,
            City.Name AS CityName
    FROM
        City
    GROUP BY City.Country) AS MaxCity
        JOIN
    Country ON Country.Code = MaxCity.Country;

这哪里出错了?

4

2 回答 2

2

以这种方式尝试(我添加了一个额外的连接)

SELECT 
    Country.Name, City.CityName, City.Population
FROM
    Country Join City On Country.Code = City.Country
Join (SELECT 
        MAX(Population) AS Population,Country as Country from City Group By Country)  as X
On City.Country = x.Country and City.Population = x.Population
于 2013-05-05T10:45:25.690 回答
1

在源查询中需要按字段 city.name 添加到组中

    select country.name, maxcity.cityname, maxcity.population
    from
        (select 
            MAX(population) as population,
        city.country,
        city.name as cityname
    from city
    group by city.country, city.name) as maxcity
            join
    country on country.code = maxcity.country
于 2013-05-06T01:57:20.960 回答