[英]postgreSQL how to convert multiple rows from a table to an array of JSON
[英]Convert rows of one table to json and add it to another table PostgreSQL
我有兩個表progress_report和progress_report_activity。
CREATE TABLE PROGRESS_REPORT
(
REPORT_ID VARCHAR NOT NULL,
PCP_PATIENT_ID VARCHAR NOT NULL,
VC_PRODUCT_ID VARCHAR ,
REPORT_DURATION_DAYS INTEGER NOT NULL,
REPORT_GENERATION_DATE BIGINT,
IS_REPORT_READ BOOLEAN ,
ACTIVITY_DETAILS JSONB,
SYMPTOMS_DETAILS JSONB,
PRIMARY KEY (REPORT_ID)
);
CREATE TABLE PROGRESS_REPORT_ACTIVITY
(
REPORT_ACTIVITY_ID VARCHAR NOT NULL,
REPORT_ID VARCHAR NOT NULL,
ACTIVITY_CONFIG_ID VARCHAR NOT NULL,
ACTIVITY_ASSISTANCE VARCHAR NOT NULL,
PRIMARY KEY (REPORT_ACTIVITY_ID)
)
我需要將ACTIVITY_CONFIG_ID和ACTIVITY_ASSISTANCE從PROGRESS_REPORT_ACTIVITY轉換為json,並將此json添加到PROGRESS_REPORT中的ACTIVITY_DETAILS列。
我試圖使用此查詢形成json:
SELECT ROW_TO_JSON(t) FROM (SELECT activity_config_id, activity_assistance FROM progress_report_activity pra inner join progress_report pr on pr.report_id=pra.report_id) t
但是問題PROGRESS_REPORT_ACTIVITY將為同一report_id返回多個行,這在兩個表中都很常見。
樣本數據:
insert into progress_report_activity values ('1','1','11','qwerty')
insert into progress_report_activity values ('3','1','13','qwertyDup')
insert into progress_report_activity values ('2','2','12','qwerty2')
insert into progress_report_activity values ('10','2','11','qwerty12223')
insert into progress_report values ('1','123','taltz',123,123456,true,null,null);
insert into progress_report values ('10','123','olumiant',123,123456,true, null,null)
預期的輸出:以下是兩種不同的輸出格式,您可以建議對兩者進行查詢。
1]示例progress_report行應如下所示:
1,123,taltz,123,123456,true, {"activity_config_id" : ["11","13"], "activity_assistance" : ["qwerty","qwertyDup"]}
2] sample progress_report行:
1,123,taltz,123,123456,true, {"activity" : [{"activity_config_id":"11",
"activity_assistance" : "qwerty"},
{"activity_config_id":"13",
"activity_assistance" : "qwertyDup" }]
9.2 對json的支持非常有限 。 解決方案看起來不會很好:
t=# with p as (SELECT pr.report_id,array_agg(activity_config_id) activity_config_id, array_agg(activity_assistance) activity_assistance FROM progress_report_activity pra inner join progress_report pr on pr.report_id=pra.report_id group by pr.report_id)
select row_to_json(j), report_id from p, (select activity_config_id,activity_assistance from p) j;
row_to_json | report_id
---------------------------------------------------------------------------------+-----------
{"activity_config_id":["11","13"],"activity_assistance":["qwerty","qwertyDup"]} | 1
(1 row)
現在,當您為每個report_id准備json時,對其進行更新:
t=# with p as (SELECT pr.report_id,array_agg(activity_config_id) activity_config_id, array_agg(activity_assistance) activity_assistance FROM progress_report_activity pra inner join progress_report pr on pr.report_id=pra.report_id group by pr.report_id)
, d as (select row_to_json(j), report_id from p, (select activity_config_id,activity_assistance from p) j)
update progress_report set ACTIVITY_DETAILS = row_to_json
from d where progress_report.report_id = d.report_id
;
UPDATE 1
更新以達到第二個預期結果,我不限於9.2(顯然OP具有9.6):
with c as (select report_id,json_agg(json_build_object('activity_config_id',activity_config_id,'activity_assistance',activity_assistance)) AS j from progress_report_activity group by report_id)
update progress_report pr set ACTIVITY_DETAILS = concat('{"activity" :',j,'}')::json
from c where c.report_id = pr.report_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.