簡體   English   中英

從 SQL 表中的 JSON 列檢索數據

[英]Retrieving data from JSON column in SQL table

我想知道如何從 SQL 中的 json 列中檢索數據。 REGEXP_SUBSTR可能工作或者是一個在Vertica的表作為它的。 我是新手,非常感謝任何可以幫助我了解如何操作的幫助,例如:

  • 查找特定 OFFICE_ID 的 prem_amount

我應該像這里這樣將整個結構分成更小的部分來理解結構嗎?

[{"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":{}}]

謝謝你的幫助!

您在此處提供的 json 格式錯誤。 您可以使用在線 json 驗證器快速檢查。 有幾個網站可供您使用。 例如: JSON 格式化程序

可以在此處找到有關查詢 json 數據的更多信息: SQL Server 中的 JSON 數據

您是如何將 JSON 數據導入 Vertica 的? 你把它放到一個 VARCHAR 中嗎? 這並不理想,因為您無法輕松驗證它(如本答案所示)或從中提取值。 相反,您可以使用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();

(猜猜我在試驗什么數據。:-))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM