我有一张表如下所示我想要实现的是输出权重高于该性别平均值的客户,所以我只会将它们与同性进行比较,下面唯一匹配的是c3和c6因为他们超过了这个。
cust_id - weight - gender
c1 - 70 m
c2 - 70 m
c3 - 71 m
c4 - 60 f
c5 - 60 f
c6 - 61 f
所以我希望这个表看起来像下面的
cust_id - weight - gender
c3 - 71 m
c6 - 61 f
以下是我到目前为止的情况但是这并没有考虑到同一性别比较,我认为这只是比较整个专栏的平均值,任何人都可以在我出错的地方伸出援助之手吗?非常感谢。
select
cust_id,
gender,
weight
from customer c
GROUP BY c.cust_id, c.gender, c.weight
HAVING AVG(weight) > (SELECT AVG(weight) FROM customer)
order by c.gender
答案 0 :(得分:2)
尝试使用此查询
select
cust_id,
gender,
weight
from customer c
WHERE weight > (SELECT AVG(weight)
FROM customer
WHERE gender = c.gender)
order by c.gender