0

试图理解这种行为

 CREATE TABLE tab (x DECIMAL(10,0), y DECIMAL(10,0))
 INSERT INTO tab VALUES(1, 3)

select x / y from tab将根据Redshift docs中的规则将 0.3333.... 返回到小数点后 11 位。

我不明白的是:如果我除以转换为相同类型的文字(x / 3::decimal(10,0)),我得到精确的 4 位小数(0.3333

除了首先将商写入表格或更糟糕的情况下,如何使用float

4

1 回答 1

2

使用您的示例选项卡表

select x / y,
       ((x/ 3::decimal)),
       ((x/ (select 3)::decimal)),
       ((x/ (select 3)::decimal))::decimal(10,6)
       from tab;

返回

0.33333333333 0.3333 0.3333333333333333333 0.333333

您在第三个示例中看到,将 3::decimal 替换为 (select 3)::decimal 会大大提高精度。

如果您需要设置输出精度,您可以像我的第 4 个示例中那样执行此操作((x/ (select 3)::decimal))::decimal(10,6)

于 2019-03-12T07:53:00.397 回答