SELECT DISTINCT( m.pm_name )
FROM prime_minister AS m,
prime_minister AS p
WHERE m.state_born IS NOT NULL
GROUP BY m.pm_name,
m.birth_yr
HAVING m.birth_yr = (SELECT Min(p.birth_yr)
FROM prime_minister)
我的问题如下:
该查询应该产生出生于澳大利亚的第一位总理......因此WHERE m.state_born IS NOT NULL
答案 0 :(得分:1)
为什么要在子查询中引用外部表?写一个像这样的简单子查询。
SELECT DISTINCT pm_name
FROM prime_minister
WHERE state_born IS NOT NULL
AND birth_yr = (SELECT MIN(birth_yr) FROM prime_minister)
答案 1 :(得分:0)
不确定我完全理解你的问题。但我根本没有找到group by
或having
的原因:
SELECT m.pm_name
FROM prime_minister
WHERE m.state_born IS NOT NULL
AND m.birth_yr = (SELECT Min(p.birth_yr)
FROM prime_minister p
WHERE p.state_born IS NOT NULL)
答案 2 :(得分:0)
这有用吗?
SELECT TOP 1 pm_name, MIN(birth_yr)
FROM prime_minister
WHERE birth_yr IS NOT NULL
GROUP BY pm_name
ORDER BY pm_name, MIN(birth_yr)
答案 3 :(得分:0)
我认为你正试图找到在每个州出生的第一位总理:
select pm_name, state_born from prime_minister m
inner join
(
select min(birth_yr) birth_yr, state_born
from prime_minister
where state_born is not null
group by state_born
)
t
on m.state_born = t.state_born and m.birth_yr = t.birth_yr
答案 4 :(得分:0)
使用EXISTS()运算符
尝试选项SELECT m.pm_name
FROM prime_minister AS m
WHERE EXISTS (
SELECT 1
FROM prime_minister p
WHERE p.state_born IS NOT NULL
HAVING MIN(p.birth_yr) = m.birth_yr
) AND m.state_born IS NOT NULL