错误 42883:运算符不存在:文本/文本

ERROR 42883: operator does not exist: text / text

当运行以下查询时:

select data from example
where (data -> 'properties' ->> 'outageCount') / (data -> 'properties' ->> 'trackedCount') > 0.01

我收到以下错误:

ERROR 42883: operator does not exist: text / text
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

outageCounttrackedCount 都作为整数存储在 JSONB 中。

我尝试使用 as float 进行转换,但出现以下错误:

[42601] ERROR: syntax error at or near "as"

即使该字段是 JSON 数字,它也将返回为 text(参见 json and jsonb operators):

db=# select pg_typeof(('{"foo":3}'::jsonb)->>'foo');
 pg_typeof
-----------
 text
(1 row)

您需要通过将 ::float 附加到表达式来转换它:

db=# select pg_typeof((('{"foo":3}'::jsonb)->>'foo')::float);
    pg_typeof
------------------
 double precision
(1 row)

你的情况:

select data from example
where (data -> 'properties' ->> 'outageCount')::float / (data -> 'properties' ->> 'trackedCount')::float > 0.01