我正在尝试创建一个查询,以从性别平均身高中获取更高的人的名字。
我知道如何获得每个性别的平均身高:
SELECT avg(height), gender from client group by gender
但是我不知道如何将每个人(基于其性别)与此子查询进行比较。
这是我所能做的:
SELECT cname
FROM (SELECT height, cname, gender
FROM client AS PplHeight
HAVING (height > (SELECT avg(height) from client group by gender))) AS AboveAvg
谢谢。
答案 0 :(得分:1)
可以加入:
select client.*
from client
join
(
select gender, avg(height) as avg_height
from client
group by gender
) genders on client.gender = genders.gender and client.height > genders.avg_height;
或使用相关子查询:
select *
from client
where height >
(
select avg(height)
from client all_clients
where all_clients.gender = client.gender
);
答案 1 :(得分:0)
您可以尝试子查询,如下所示:
select *
from client as c inner join
(
SELECT avg(height) as avgh, gender
from client
group by gender
) as t
on c.gender = t.gender and c.height > t.avgh
答案 2 :(得分:0)
您可以在性别和身高更大的对象上内部联接子查询。
SELECT c1.cname
FROM client c1
INNER JOIN (SELECT c2.gender,
avg(c2.height) height
FROM client c2
GROUP BY c2.gender) x
ON x.gender = c1.gender
AND x.height > c1.height;
答案 3 :(得分:0)
SELECT PplHeight.cname, PplHeight.gender, PplHeight.height
FROM
client AS PplHeight,
(SELECT gender, avg(height) as avg_height from client group by gender) avg_h
WHERE PplHeight.gender = avg_h.gender
AND PplHeight.height > avg_h.avg_height