刚刚安装了9.4并尝试使用JSONB字段类型。
我用jsonb字段创建了一个表,并且可以从中选择:
select statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as profitable_perc FROM trade_statistics
工作正常。
现在我想根据字段值过滤结果:
select statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as profitable_perc FROM trade_statistics WHERE profitable_perc > 1
//There is no "profitable_perc" column
不起作用。
如果我尝试将结果转换为double,则也不起作用。
select cast(statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as double precision) as profitable_perc FROM trade_statistics
//cant convert jsonb into double precision
在jsonb的情况下,如何在WHERE子句中使用select结果?
答案 0 :(得分:3)
必须进行三次更正:
SELECT
子句中引用WHERE
列表别名->>
运算符将值作为文本将文本值转换为整数,以便进行比较
SELECT *
FROM (
SELECT (statistics->'statistics'->'all_trades'->'all'->>'all_trades_perc_profit')::integer as profitable_perc
FROM trade_statistics
) sq1
WHERE profitable_perc > 1