SQL如何获取已创建列的平均值

时间:2018-10-10 02:41:37

标签: sql ms-access

我有这个查询

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

但是我得到一个错误,它不起作用。

1 个答案:

答案 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;