ОШИБКА 42883: оператор не существует: текст / текст - PullRequest
0 голосов
/ 10 июня 2019

При выполнении следующего запроса:

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.

И outageCount, и trackedCount хранятся как целые числа в JSONB.

Я пробовал использовать кастинг, используя as float, но он выдал следующую ошибку:

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

1 Ответ

1 голос
/ 10 июня 2019

Даже если поле является числом JSON, оно будет возвращено как text (см. операторы json и jsonb ):

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
...