Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Jun 30, 2000

Calculating the Average of a Column

Question:
I'm doing a Web front for a database that contains data about temperature. How do I calculate an average temperature using SQL code?

Answer:
Use the AVG function to get an average of all the values in a single column. With a table called TestTable and this data:

Temperature          
-------------------- 
65
65
32
78
80
56
This query will return an average of all the rows:
SELECT AVG(Temperature) AS AvgTemp
FROM TestTable

AvgTemp                                  
---------------------------------------- 
62.666666
In this case, the Temperature column is of data type decimal. If, for some reason, your Temperature column was data type int and you want the same results, use the CAST function like this:
SELECT AVG(CAST(Temperature AS decimal))
FROM TestTable
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date