75 pts.
 SQL Server 2005 query to get the average
Hi, I'm using MSSql 2005 on Windows 2003 Server. I need some assistance to write a query to get the average of two columns and write the value to a third coulmn. All the transactions is in one table. Basically it is column 1 + column 2 devide by two = column 3 Thank you, George

Software/Hardware used:
ASKED: February 24, 2009  5:43 AM
UPDATED: February 24, 2009  6:38 PM

Answer Wiki:
You you do the match pretty much like you wrote it above. <pre>SELECT (Column1+Column2)/2 as Column3 From YourTable</pre> <pre>Problem is that I run the query and it dispays my value, but asoon as I query that column the value is NULL.</pre>
Last Wiki Answer Submitted:  February 24, 2009  8:03 am  by  GPNel2006   75 pts.
All Answer Wiki Contributors:  GPNel2006   75 pts. , Denny Cherry   64,505 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Are you asking for a “statement” that sets the third column value to the average of the other two columns? That isn’t a “query” – you need to use an Update statement:

update mytable set column3 = (column1 + column2)/2;

A query does not change the values in the underlying tables.

Note – you may want to look at functions like “Round” or “Floor” if you want the average to be of a specified precision, or an integer value.

update mytable set column3 = round((column1+column2)/2, 2);

 3,830 pts.