从SQL表的JSON列中检索数据

时间:2018-08-30 09:15:43

标签: sql json vertica

我想知道如何从SQL的json列中检索数据。 regexp_substr可以工作,也可以that one作为其在Vertica表中的工作。我是新手,非常感谢您能提供帮助的任何帮助,例如:

  • 找到特定OFFICE_ID的prem_amount

我应该在下面将整个结构分成较小的like here来理解结构吗?

[{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["1"],"intervals":[{"end_offset":464400,"start_at":null,"start_offset":457200,"end_at":null}],"id":"1","OFFICE_ID":"1","content_hash_id":"1","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["2"],"intervals":[{"end_offset":550800,"start_at":null,"start_offset":543600,"end_at":null}],"id":"2","OFFICE_ID":"2","content_hash_id":"2","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["3"],"intervals":[{"end_offset":471600,"start_at":null,"start_offset":464400,"end_at":null}],"id":"3","OFFICE_ID":"3","content_hash_id":"3","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["4"],"intervals":[{"end_offset":558000,"start_at":null,"start_offset":550800,"end_at":null}],"id":"4","OFFICE_ID":"4","content_hash_id":"4","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["5"],"intervals":[{"end_offset":478800,"start_at":null,"start_offset":471600,"end_at":null}],"id":"5","OFFICE_ID":"5","content_hash_id":"5","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["6"],"intervals":[{"end_offset":565200,"start_at":null,"start_offset":558000,"end_at":null}],"id":"6","OFFICE_ID":"6","content_hash_id":"6","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["7"],"intervals":[{"end_offset":486000,"start_at":null,"start_offset":478800,"end_at":null}],"id":"7","OFFICE_ID":"7","content_hash_id":"7","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["8"],"intervals":[{"end_offset":572400,"start_at":null,"start_offset":565200,"end_at":null}],"id":"8","OFFICE_ID":"8","content_hash_id":"8","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["9"],"intervals":[{"end_offset":493200,"start_at":null,"start_offset":486000,"end_at":null}],"id":"9","OFFICE_ID":"9","content_hash_id":"9","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["10"],"intervals":[{"end_offset":579600,"start_at":null,"start_offset":572400,"end_at":null}],"id":"10","OFFICE_ID":"10","content_hash_id":"10","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["11"],"intervals":[{"end_offset":500400,"start_at":null,"start_offset":493200,"end_at":null}],"id":"11","OFFICE_ID":"11","content_hash_id":"11","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["12"],"intervals":[{"end_offset":586800,"start_at":null,"start_offset":579600,"end_at":null}],"id":"12","OFFICE_ID":"12","content_hash_id":"12","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}}]

谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您在此处提供的json格式错误。您可以使用在线json验证器快速检查。您可以使用几个网站。 例如:JSON Formatter

有关查询json数据的更多信息,请参见:JSON data in SQL Server

答案 1 :(得分:1)

您是如何将JSON数据导入Vertica的?您是否将其放入VARCHAR?这并不理想,因为您无法轻松地对其进行验证(如this answer所示)或从中提取值。相反,您可以使用FJSONParser加载JSON数据。例如:

vsql> create table super(age int, name varchar);

vsql> copy super from stdin parser fjsonparser();
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> {"age": 5, "name": "Tim"}
>>  {"age": 3}
>>  {"name": "Fred"}
>>  {"name": "Bob", "age": 10}
>> \.

vsql> select * from super;
 age | name
-----+------
     | Fred
  10 | Bob
   5 | Tim
   3 |
(4 rows)

很显然,您不会在STDIN上键入JSON;那只是文档中的示例。您可以像这样从JSON文件加载:

vsql> COPY SE.PostLinks FROM '"$loaddir"/PostLinks.json' PARSER fjsonparser();

(猜猜我正在实验什么数据。:-))