我有这个查询
SELECT stock,
ROUND(AVG(dividend / price) *100,3) AS yield
FROM stock
GROUP BY stock
我正在尝试获取平均收益率并显示所有收益率高于平均收益率的股票。
我尝试过
SELECT stock,
ROUND(AVG(dividend / price) *100,3) AS yield
FROM stock
WHERE yield > AVG(yield)
GROUP BY stock
但是我得到一个错误,它不起作用。
答案 0 :(得分:0)
使用子查询,join
可能是最简单的:
SELECT s.*
FROM (SELECT stock,
ROUND(AVG(dividend / price) * 100, 3) AS yield
FROM stock
GROUP BY stock
) as s INNER JOIN
(SELECT AVG(yield) as avg_yield
FROM (SELECT stock,
ROUND(AVG(dividend / price) * 100, 3) AS yield
FROM stock
GROUP BY stock
) as s
) as ss
ON s.yield >= s.avg_yield;