mysql - MYSQL - How to match the country with the highest deaths for a unique list of continents
问题描述
I am trying to find the country with the highest number of deaths in each continent. So far, I have the following code which gives me the continent and the highest number of deaths, but I am unable to get the country, as shown in the image 'Code without Country'
SELECT
continent,
MAX(total_deaths) as AllDeaths
FROM covid.covid_
WHERE continent IS NOT NULL
GROUP BY continent
ORDER BY 2 DESC;
However, if I add country to my code, I no longer have what I need
SELECT
continent,
country,
MAX(total_deaths) as AllDeaths
FROM covid.covid_
WHERE continent IS NOT NULL
GROUP BY continent, country
ORDER BY 3 DESC;
As you can see in the 2nd image, now I have multiple results for each continent, which is not what I need - I need a unique list of all continents, along with the country contributing to the highest deaths in each continent
解决方案
select b.continent, b.country, a.total_deaths
from (
SELECT
continent,
MAX(total_deaths) as AllDeaths
FROM covid.covid_
WHERE continent IS NOT NULL
GROUP BY continent) as a
inner join covid.covid_ as b on a.total_deaths = b.total_deaths and a.continent = b.continent
推荐阅读
- python - 如何在 RHEL 上安装 rpm 和依赖项?
- javascript - 如何创建较短版本的视差?
- angular - SignalR Hubconnection 未收到来自 Azure 函数应用的任何消息
- python - 对时间敏感的应用程序的并发、并行和多处理
- node.js - 当我从更下方的文件夹中删除空的 package.json 文件时,Webpack 构建中断
- angular - 如何使用嵌套数据扩展角度材料树中的嵌套节点?
- python - 比特币在 python 中验证单个块
- sql - 为列中的每个值创建子查询
- javascript - 错误:在 webpack 配置版本 5 中使用 devtool="source-map" 选项时
- python - 从 Jupyter Notebook 运行 Spark/Python