无法创建子查询

esbemjvw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(256)

如果我有以下数据:


# EducationalAttainment, age, sum(population_count)

'Bachelor\'s degree or higher', '00 to 17', '14673'
'Bachelor\'s degree or higher', '18 to 64', '46032757'
'Bachelor\'s degree or higher', '65 to 80+', '8570246'
'High school or equivalent', '00 to 17', '114881'
'High school or equivalent', '18 to 64', '35577621'
'High school or equivalent', '65 to 80+', '7250424'
'No high school diploma', '00 to 17', '9566523'
'No high school diploma', '18 to 64', '25353234'
'No high school diploma', '65 to 80+', '5749114'
'Some college, less than 4-yr degree', '00 to 17', '91542'
'Some college, less than 4-yr degree', '18 to 64', '44843283'
'Some college, less than 4-yr degree', '65 to 80+', '7202669'

我会写什么样的查询,把每个年龄组的人口加起来,然后计算出一个分数,比如说,那些拥有学士学位的人从00到17岁,占了这00到17岁的总人口的一小部分。
这来自以下查询:

select EducationalAttainment, age, sum(population_count) 
from educational_attainment
group by EducationalAttainment, age;
k2arahey

k2arahey1#

必须将子查询与join一起使用:

SELECT A.EducationalAttainment, A.age, A.population_count/B.age_population_count population_fraction
FROM 
    educational_attainment A
JOIN
    (SELECT age, sum(population_count) age_population_count
    FROM educational_attainment
    GROUP BY age) B
ON A.age=B.age;

相关问题