我想列出所有年龄大于具有相同国籍的人的平均年龄的人。涉及三个表格:人员,护照和国家。这是我到目前为止有:oracle sql:比具有相同国籍的人的平均年龄早的人
select round(months_between(sysdate, dob)/12) as age, country.name, person.name, person.surname
from person
join passport on person.pid= passport.pid
join country on passport.cid= country.cid
where round(months_between(sysdate, dob)/12) >
(select avg(round(months_between(sysdate, dob)/12))
from person join passport on person.pid= passport.pid
join country on passport.cid= country.cid);
出于某种原因,我得到它小于人的平均年龄不想要的结果。对于同一国籍的人的平均年龄的select语句是:
SELECT avg(round(months_between(sysdate, dateofbirth)/12)) as age, country.name
from person
join passport on person.personid = passport.personid
join country on passport.countryid = country.countryid
group by country.name;
我用这个没有GROUP BY子句我再选择在上述范围内。这可以正常工作,但只有年龄大于平均年龄的人才能正确地查询结果。
提示;子查询中有太多连接。它应该与外部查询相关联。 –
我从_subselect_中删除了所有连接,但仍然在输出中显示不需要的结果。 @GordonLinoff –