Richard Quinn
Richard Quinn

Reputation: 232

SQL Query Average

I am trying to work out a sql query to work out the average off 3 values to 2 decimal places.

SELECT RatingId, Productivity, CodeQuality, AgileProcess
     , ROUND(AVG(Productivity + CodeQuality + AgileProcess) / 3, 2) AS Average
FROM DeveloperRating
GROUP BY RatingId, Productivity, CodeQuality, AgileProcess`

Productivity, CodeQuality, AgileProcess are all values currently set in DB.

If the values are set as follows: Productivity = 2 CodeQuality = 8 AgileProcess = 4

I get the answer 4. I would like the answer to be 4.66 ?

Can anyone help?

Upvotes: 0

Views: 54

Answers (1)

Barranka
Barranka

Reputation: 21047

You don't need to use aggregate functions here! You're grouping for each row in your table. Other than that, you may need to cast your values to DOUBLE to get what you need.

Try:

 select ratingId, productivity, codeQuality, agileProcess
      , round(cast(productivity as double) + 
              cast(codeQuality as double) + 
              cast(agileProcess as double) / 3,2)
 from developerRating

Upvotes: 1

Related Questions