无限制地选择最高值
Selecting top values without limit
我有以下关系模式:
Country(code: Str, name: Str, capital: Str, area: int)
code (this is the usual country code, e.g. CDN for Canada, F for France, I for Italy)
name (the country name) capital (the capital city, e.g. Rome for Italy)
area (The mass land the country occupies in square Km)
Economy (country: Str, GDP: int, inflation: int, military: int, poverty: int)
country is FK to the Country table
GDP (gross domestic product)
inflation (annual inflation rate)
military (military spending as percentage of the GDP)
poverty rate (percentage of population below the poverty line)
Language (country: Str, language: Str, percentage: int)
country is FK to the Country table
language is a spoken language name
percentage (percentage of population speaking the language)
我需要编写一个查询,找出使用最多语言的country/countries的贫困率。
我写了这个查询
SELECT poverty
FROM(
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country) AS conto
JOIN "Economy" AS E
ON E.country=conto.country
ORDER BY conto.langnum DESC
LIMIT 1
显然只有当我有一个国家拥有最大语言数量时它才有效,如果有多个国家拥有最大语言数量我该怎么办?
使用rank()
或dense_rank()
:
SELECT poverty
FROM (SELECT COUNT(language) as langnum, country,
RANK() OVER (ORDER BY COUNT(language) DESC) as ranking
FROM "Language"
GROUP BY country
) conto JOIN "Economy" AS E
ON E.country=conto.country
WHERE conto.ranking = 1
ORDER BY conto.langnum DESC;
只需添加另一个返回最大语言数的子选择:
SELECT poverty, E.country
FROM(
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country) AS conto
JOIN "Economy" AS E
ON E.country=conto.country
JOIN (
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country ORDER BY 1 DESC LIMIT 1
) AS maxlang ON maxlang.langnum = conto.langnum
ORDER BY conto.langnum DESC
我有以下关系模式:
Country(code: Str, name: Str, capital: Str, area: int)
code (this is the usual country code, e.g. CDN for Canada, F for France, I for Italy)
name (the country name) capital (the capital city, e.g. Rome for Italy)
area (The mass land the country occupies in square Km)
Economy (country: Str, GDP: int, inflation: int, military: int, poverty: int)
country is FK to the Country table
GDP (gross domestic product)
inflation (annual inflation rate)
military (military spending as percentage of the GDP)
poverty rate (percentage of population below the poverty line)
Language (country: Str, language: Str, percentage: int)
country is FK to the Country table
language is a spoken language name
percentage (percentage of population speaking the language)
我需要编写一个查询,找出使用最多语言的country/countries的贫困率。
我写了这个查询
SELECT poverty
FROM(
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country) AS conto
JOIN "Economy" AS E
ON E.country=conto.country
ORDER BY conto.langnum DESC
LIMIT 1
显然只有当我有一个国家拥有最大语言数量时它才有效,如果有多个国家拥有最大语言数量我该怎么办?
使用rank()
或dense_rank()
:
SELECT poverty
FROM (SELECT COUNT(language) as langnum, country,
RANK() OVER (ORDER BY COUNT(language) DESC) as ranking
FROM "Language"
GROUP BY country
) conto JOIN "Economy" AS E
ON E.country=conto.country
WHERE conto.ranking = 1
ORDER BY conto.langnum DESC;
只需添加另一个返回最大语言数的子选择:
SELECT poverty, E.country
FROM(
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country) AS conto
JOIN "Economy" AS E
ON E.country=conto.country
JOIN (
SELECT COUNT(language) as langnum, country
FROM "Language"
GROUP BY country ORDER BY 1 DESC LIMIT 1
) AS maxlang ON maxlang.langnum = conto.langnum
ORDER BY conto.langnum DESC