Skip to content
Advertisement

Changing data type to float and rounding to 2 decimal digits

Tables:

Goal: Display each unique job, the total average salary (FLOAT and rounded to 2 decimal places), the total people and the total salary (Float and rounded to 2 decimal places) and order by highest average salary.

So the challenge is to keep the cast type as float while rounding it to 2 decimal places.

I’ve gotten to where I’ve rounded it 2 decimal places but it’s not float. I’ve gotten it to where it’s float but I can’t round it to 2 decimal places.

My Attempts:

Attempt 1:

Problem: Still says it’s not float

Attempt 2:

Problem: not rounded to 2 decimal places

Attempt 3:

I get an error saying I need to add explicit cast types which led me to attempt number 1.

Advertisement

Answer

The answer depends on the actual datatype of column salary. The key point is that round() in Postgres does not allows floats (only numeric types are supported).

If you are dealing with a numeric datatype, then you can first round(), then cast to float:

If you are dealing with a float column, then you would need to cast the result of the aggregate function before using round() on it:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement