Jatin Jatin - 3 months ago 8
SQL Question

Adding 3 columns (that have avg() values) in 4th column

I have a table which has 3 columns; TimeRetrieval, TimePorcessing and TimeRendering. All these 3 column displays the avg values. Now, I want to add these avg value and display it in 4th column named "AvgTotalTime".
What my query looks like now is:

Select
avg(a.TimeRetrieval) as "Retrieval time",
avg(a.TimeProcessing) as "Processing time",
avg(a.TimeRendering) as "Rendering Time",
(avg(a.TimeRetrieval)+avg(a.TimeProcessing)+avg(a.TimeRendering)) AS "AvgTotalTime"
from TimeLog a


I know there's something wrong with the addition of 3 columns, but can't figure out the corerct syntax to do it.

Appreciate your help.

Thanks

Answer

Try this

SELECT [Retrieval time],
       [Processing time],
       [Rendering Time],
       [Retrieval time]+[Processing time]+[Rendering Time] AS "AvgTotalTime"
FROM 
( 
     Select 
     avg(a.TimeRetrieval) as "Retrieval time",
     avg(a.TimeProcessing) as "Processing time",
     avg(a.TimeRendering) as "Rendering Time"
     from TimeLog a
 )M 
Comments