AVG()
是一个聚合函数,它从多行中获取值并给出它们的平均值。
您正在尝试平均 3 columns。
在您的具体情况下,可以使用代数来完成。
SELECT
(SUM(score) * .3) As score_a,
(SUM(score) * .6) As score_b,
(SUM(score) * .8) As score_c,
(SUM(score) * ((0.3 + 0.6 + .8) / 3.0)) As score_avg
FROM
score_table
在更一般的情况下,您会受到以下事实的限制:您无法引用刚刚在另一列中定义的列......
SELECT
a + 1 AS inc_a,
inc_a * 2 AS this_is_invalid
FROM
your_table
您要么需要重复自己,要么使用子查询...
重复
SELECT
(SUM(score) * .3) As score_a,
(SUM(score) * .6) As score_b,
(SUM(score) * .8) As score_c,
((SUM(score) * .3) + (SUM(score) * .3) + (SUM(score) * .3)) / 3.0 As score_avg
FROM
score_table
子查询
SELECT
score_a,
score_b,
score_c,
(score_a + score_b + score_c) / 3.0 AS score_avg
FROM
(
SELECT
(SUM(score) * .3) As score_a,
(SUM(score) * .6) As score_b,
(SUM(score) * .8) As score_c
FROM
score_table
)
AS data