I have the following code below and I’m trying to get an AVG for the 3 columns.
SELECT
(SUM(score) * .3) As score_a,
(SUM(score) * .6) As score_b,
(SUM(score) * .8) As score_c
--now I want to get the AVG of the above scores
AVG(score_a + score_b + score_c) As avg_score
FROM score_table
but this does not work. The error I’m getting is “Invalid column name score_a”.
I’m using SQL Server 2008
AVG()is an aggregate function that takes values from multiple rows and gives their average.You’re trying to average 3 columns.
I your specific case, it can be done using algebra.
In a more generalised case, you’re limited by the fact that you can’t reference a column you just defined in another column…
You’d either need to repeat yourself, or use a sub query…
Repetition
Sub Query