How to round an average to 2 decimal places in PostgreSQL?

后端 未结 8 1477
渐次进展
渐次进展 2020-11-30 18:01

I am using PostgreSQL via the Ruby gem \'sequel\'.

I\'m trying to round to two decimal places.

Here\'s my code:

SELECT ROUND(AVG(some_column)         


        
相关标签:
8条回答
  • 2020-11-30 18:31

    Try casting your column to a numeric like:

    SELECT ROUND(cast(some_column as numeric),2) FROM table
    
    0 讨论(0)
  • 2020-11-30 18:33

    Try with this:

    SELECT to_char (2/3::float, 'FM999999990.00');
    -- RESULT: 0.67
    

    Or simply:

    SELECT round (2/3::DECIMAL, 2)::TEXT
    -- RESULT: 0.67
    
    0 讨论(0)
提交回复
热议问题