我们在表party_procedures
中有一个json列'passed_states_data'列数据如下所示:
[{“state”:“documents”,“passed_at”:“2016-02-04 13:16:51 +0100“},{”state“:”problems“,”passed_at“:”2016-02-04 16:10:07 +0100“},{”state“:”waiting_for_physical_documents“,”passed_at“:”2016- 02-08 十三时五十分17秒 +0100“},{”state“:”customer_acceptance“,”passed_at“:”2016-02-08 13:50:19 +0100“},{”state“:”customer_identifier“,”passed_at“:”2016-02-08 13:50:54 +0100“},{”state“:”checking_account“,”passed_at“:”2016- 02-08 13时51分45秒 0100 “},{” 状态 “:” transfer_money_to_savings_account “ ”passed_at“:” 2016年2月10日 09:12:18 +0100“}]
我需要构建只返回存在'transfer_money_to_savings_account'的行的查询,然后获取pass_at日期。
所以查询应该返回如下内容:
select blah as "date passed" from party_procedures pp
where json_array_elements(pp.passed_states_data)->>'state'='transfer_money_to_savings_account' = 't'
或类似的东西。
答案 0 :(得分:0)
json_array_elements()
函数返回 json 集合,因此,您无法使用->>
;换句话说,您需要迭代json数组元素集并检查state
是否具有您要查找的值。
WITH t(passed_states_data) AS ( VALUES
('[{"state":"documents","passed_at":"2016-02-04 13:16:51 +0100"},
{"state":"problems","passed_at":"2016-02-04 16:10:07 +0100"},
{"state":"waiting_for_physical_documents","passed_at":"2016-02-08 13:50:17 +0100"},
{"state":"customer_acceptance","passed_at":"2016-02-08 13:50:19 +0100"},
{"state":"customer_identifier","passed_at":"2016-02-08 13:50:54 +0100"},
{"state":"checking_account","passed_at":"2016-02-08 13:51:45 +0100"},
{"state":"transfer_money_to_savings_account","passed_at":"2016-02-10 09:12:18 +0100"}]'::JSON)
)
SELECT j->>'passed_at' as "date passed" FROM t, json_array_elements(t.passed_states_data) j
WHERE j->>'state' = 'transfer_money_to_savings_account';
结果:
date passed
---------------------------
2016-02-10 09:12:18 +0100
(1 row)