PostgreSQL将相同的值视为不同的值

时间:2016-11-08 14:11:06

标签: sql postgresql floating-point comparison precision

所以在这个例子中,SQL返回的行包含#34; quantdade"和" quantidade_reservada",每个包含值" 110",输入双精度:

...

当我尝试应用条件来仅检索结果时," quantdade"不等于" quantidade_reservada",PostgreSQL似乎没有区分数字。

110与110有何不同?

2 个答案:

答案 0 :(得分:0)

Floating-Point Types

  

数据类型实数和双精度是不精确的,可变精度数值类型。

尝试

where round(quantidade::numeric, 2) != round(quantidade_reservada::numeric, 2)

Arbitrary Precision Numbers

答案 1 :(得分:0)

数字的值与pgAdmin中的表示之间存在差异。
你在pgAdmin中看到的是一个圆形显示值到2位数的比例。

with t (x,y) as (select 102/300::numeric(23,20),101/300::numeric(23,20)) 

select  x
       ,y
       ,case when x=y then 'Y' else 'N' end     as is_equale
       ,x-y                                     as "x-y"
       ,(x-y)*1000                              as "(x-y)*100"

from    t
;

pgAdmin结果:

x       y       is_equale   x-y     (x-y)*100
----    ----    ---------   ----    ---------
0.34    0.34    N           0.00    3.33

DbVisulizer结果:

x                       y                       is_equale   x-y                     (x-y)*100
----------------------  ----------------------  ---------   ----------------------  ----------------------
0.34000000000000000000  0.33666666666666666667  N           0.00333333333333333333  3.33333333333333333000
相关问题