Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve decimals when rounding an average in SQL

Here is my SQL statement

SELECT ROUND(AVG(column_name), 100)  FROM [database].[dbo].[table] 

The answer I retrieve is 3 with a possible range of numbers between 1 - 5 in 908 rows.

I need it to say 3.0 or 3.09. Is this possible?

like image 746
Skullomania Avatar asked Nov 05 '12 17:11

Skullomania


People also ask

How do I get the average of a decimal in SQL?

SQL CAST() inside AVG() for decimal valueThe SQL AVG() function returns the average value with default decimal places. The CAST() is used to increase or decrease the decimal places of a value. The CAST() function is much better at preserving the decimal places when converting decimal and numeric data types.

How do I stop rounding decimals in SQL?

using this Custom format : #,0.

How do I extract decimal parts in SQL?

X - TRUNC(X), works for negatives too. It would give you the decimal part of the number, as a double, not an integer.

How do you round an average to the nearest integer in SQL?

If you are in SQL Server, just use round(avg(column * 1.0), 0) .


1 Answers

The average will have the same data type as the values, so cast the values:

SELECT ROUND(AVG(CAST(column_name AS FLOAT)), 2) FROM [database].[dbo].[table] 
like image 165
Guffa Avatar answered Oct 17 '22 04:10

Guffa