'Find average and then use that value to calculate other values
I have a query with a few columns. I need to find the avg of a particular column and then use the average value to calculate other values.
I am finding avg with select avg(col1) as mean from tablea
.
Now I want to use the mean value to calculate other values.
I tried:
select avg(col1) as mean, col2 - 2(avg(col1)
from table a
group by col2.
The problem is query should return only one row, but it is returning many rows based on col2
.
Solution 1:[1]
Nest query.
SELECT col2, col2 - 2*(SELECT Avg(col1) AS mean FROM tablea) AS Calc
FROM tablea
GROUP BY col2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |