我必須在20-24範圍內的最大男性數量的國家/地區顯示1行的輸出。但是,sex_ratio應該顯示所有範圍?SQL查詢簡單問題子查詢
SELECT age,male ,female,sex_ratio,country
FROM census.population
WHERE country<> 'Aggregated' and census.population.age='20-24'
ORDER BY male DESC FETCH FIRST 1 ROW ONLY;
應該是這樣的:
--*** select age, ****
--*** total number of males, ****
--*** total number of females, ****
--*** sex_ratio,(of every age) ****
--*** country ****
--*** where the country has the highest number of ****
--*** males in the 20-24 age range ****
我的輸出至今:
AGE MALE FEMALE SEX_RA COUNTRY
20-24 58275712 51794339 112.5 India
結果應該是印度,因爲它有男性在20-24的最大數量範圍與每個年齡的性別比(不僅僅是20-24)。 census.population.age ='總計'將提供每個年齡段的性別比例。我如何獲得20至24歲男性的總年齡和全國性別比?什麼是選擇正在爲sex_ratio工作? 我的想法是這樣的,但它不起作用。
SELECT age,male ,female,sex_ratio,country
FROM census.population
WHERE country<> 'Aggregated' and census.population.age='20-24'
AND sex_ratio = (select sex_ratio from census.population
WHERE census.population.age = 'Total')
ORDER BY male DESC FETCH FIRST 1 ROW ONLY;
我的數據庫
Column Name Description
REGION "WORLD"
COUNTRY "AGGREGATED" and 228 separate countries
YR "2015"
AGE "Total" and 29 separate age ranges
BOTH_SEXES AGE range number
MALE AGE range number
FEMALE AGE range number
PERCENT_BOTH_SEXES AGE range percent
PERCENT_MALE AGE range percent
PERCENT_FEMALE AGE range percent
SEX_RATIO AGE range ratio
你能否提供樣本預期產出?我很難理解你想在這裏實現什麼。 – Shadow
我更新了我的帖子。我知道我需要一個子選擇,但我卡住了。 – bastel