PostgreSQL 比较两个 float8 列数据类型

as *_* if 2 sql postgresql

我有下表items。看起来像这样

|    date    |  id |   s_px   |   c_px   | fee |
+------------+-----+----------+----------+-----+
| 2015-01-01 | 001 | 5355.00  |  5355.00 |   2 |
| 2015-01-01 | 002 | 13240.00 | 13240.00 |   3 |
| 2015-01-01 | 003 | 5840.00  |  5840.00 |   1 |
| 2015-01-01 | 004 | 20.55    |    20.59 |   5 |
| 2015-01-01 | 005 | 64.42    |    64.42 |   6 |
Run Code Online (Sandbox Code Playgroud)

s_pxc_px数据类型为 pf float8

当我进行比较s_pxc_px不平等时

select * from items where s_px != c_px
Run Code Online (Sandbox Code Playgroud)

这将返回几乎相同的原始表。我知道直接浮动比较并不容易。但有没有类似的东西numpy.isclose

Gor*_*off 6

不等式运算符是<>(or !=),因此您的查询是正确的。然而,浮点值是变化无常的。我建议检查以确保它们不在彼此的某个小范围内:

select *
from items
where abs(s_px - c_px) > 0.0001;
Run Code Online (Sandbox Code Playgroud)