SQL AVG returning an int

40,605

Solution 1

Try to do it like this:

AVG(Cast(e.employee_level as Float)) as avg_level

Also i found this topic where you can find some another approach but i not used its and don't know exactly whether works or not.

Solution 2

Casting is more certain, but ...AVG(1.0 * e.employee_level)... might do it as well, and can be more legible.

Solution 3

The returned type of the AVG depends on the evaluated type of the expression passed to the function.

enter image description here

For example, if you want your result to be float, you need to ensure your column or expression type is float.

Share:
40,605
CodeKingPlusPlus
Author by

CodeKingPlusPlus

Updated on September 03, 2021

Comments

  • CodeKingPlusPlus
    CodeKingPlusPlus over 2 years

    In one of my queries it appears that the AVG function is returning an int.

    select ..., AVG(e.employee_level)avg_level
    

    How do I get it to return floating point values? I tried casting it but all my rows for avg_level were still integers.