我不确定百分比是否有效,我已经尝试过查看类似问题。但如果它确实有用,那么我需要显示LargestCity
列。
输出应该是这样的:
Country Name - United States
LargestCity - New York
CityPopulation - 8008278
Percentage to the country population - 2.88
我有什么:
SELECT
co.Name, co.Population,
MAX(ci.Population),
((MAX(ci.Population) * 100) / Co.Population) “PercentageOfCountryPopulation”
FROM
Country co
JOIN
City ci ON (co.Code = ci.CountryCode)
WHERE
Continent = ‘North America’ OR Continent = ‘South America’
GROUP BY
Co.Name
非常感谢任何帮助
答案 0 :(得分:0)
首先,你计算百分比的方式是错误的。
示例:
要计算一个数字与另一个数字的百分比,比率(Y / X = P%):
公式:
15是百分之十五?Y / X =((Y / X)* 100)%= P%
Step 1: 15/150 = 0.10
step 2:((15/150) * 100) = 10%
查询:
使用percentage
Maximum population
SELECT co.NAME,
co.population,
max_population,
( ( co.population / max_population ) * 100 ) as percentageofcountrypopulation
FROM country co
JOIN city ci
ON co.code = ci.countrycode
INNER JOIN (SELECT countrycode,
Max(population) max_population
FROM city
GROUP BY countrycode) mc
ON mc.countrycode = co.code
WHERE continent = 'north america'
OR continent = 'south america'
如果您想在国家/地区中找到percentage
的每个城市人口的total population
,请将Max
汇总替换为Sum
聚合
(SELECT countrycode,
SUM(population) total_population
FROM city
GROUP BY countrycode)