我的数字查询输出不能正常使用无限小数。该列的类型为“数值”。除非十进制可以无限大,否则输出是很好的,如下所示:‘1.33333333333333’
查询我尝试过
CAST(sum(column) as decimal(18,2)) / 60
ROUND(sum(column),2) / 60
我使用MySQL已经有一段时间了,并且这些查询的函数在那里是正确的。我一定是遗漏了一些特别的东西。
输出
1
0
0
3
57.5
0.5
1.3333333333333333
发布于 2021-10-27 07:17:06
PostgreSQL没有定义round(double precision, integer)
。出于@Mike‘猫召回’在评论中解释的原因,只对numeric
提供精确的圆形版本。
regress=> SELECT round( float8 '3.1415927', 2 );
ERROR: function round(double precision, integer) does not exist
regress=> \df *round*
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+--------+------------------+---------------------+--------
pg_catalog | dround | double precision | double precision | normal
pg_catalog | round | double precision | double precision | normal
pg_catalog | round | numeric | numeric | normal
pg_catalog | round | numeric | numeric, integer | normal
(4 rows)
regress=> SELECT round( CAST(float8 '3.1415927' as numeric), 2);
round
-------
3.14
(1 row)
(在上面,请注意,float8
只是double precision
的缩写别名。您可以看到,PostgreSQL正在输出中扩展它)。
必须将要舍入的值转换为numeric
,才能使用圆形的双参数形式。只需为速记演员追加::numeric
,如round(val::numeric,2)
。
https://stackoverflow.com/questions/69732582
复制相似问题