合并相同 patient_id 的 JSON 数据为数组

问题

c 复制代码
select patient_id,concat('{"itemText":"',item_text,'","itemValue":"',item_value,'"}') from hs_patient_groups where active = 1;

eef41128c47c401abb7f8885a5f9fbdf	{"itemText":"旧","itemValue":"2"}
eef41128c47c401abb7f8885a5f9fbdf	{"itemText":"新","itemValue":"1"}
0564ccc4c07b4df98bf8207f7084b493	{"itemText":"患者1组","itemValue":"patientGroup1"}
e92acabeb871422dacd11199df89bbc3	{"itemText":"患者1","itemValue":"患者1"}
af2b9bb388af484fb99c9d03cc2041ca	{"itemText":"新","itemValue":"1"}
5e4ff36bebd7478892978f875f48f515	{"itemText":"患者1","itemValue":"患者1"}
344035419b3a4c4e8b29d76c627679f9	{"itemText":"患者2","itemValue":"患者2"}
bc266205ca604150b3d800aab059fba8	{"itemText":"患者2","itemValue":"患者2"}
1801a4524b084d2b8197bfcc33849b24	{"itemText":"患者1","itemValue":"患者1"}
1801a4524b084d2b8197bfcc33849b24	{"itemText":"患者2","itemValue":"患者2"}
73cd39179ab147e6968858aecd56f214	{"itemText":"冠心病组","itemValue":"02"}
d54ced0ccbba4bb2b1d3359877fb0620	{"itemText":"瓣膜组","itemValue":"01"}
3c92057be370414b822407b1ca89ad1b	{"itemText":"瓣膜组","itemValue":"01"}
591883a377d6428f985bdb99b762929b	{"itemText":"瓣膜组","itemValue":"01"}
2b1b8ac08c5d4ac39625c20bd206ed00	{"itemText":"患者2","itemValue":"患者2"}
19822b6d38504d1a8af94dbfa32c2c0e	{"itemText":"患者2","itemValue":"患者2"}
ab62ee95526e405e93f117a25db77eee	{"itemText":"瓣膜组","itemValue":"01"}
181e27e744e242719a47d5a86260eeb1	{"itemText":"冠心病组","itemValue":"02"}
e1d862aa6baa474987545776ad8e84db	{"itemText":"冠心病组","itemValue":"02"}

如何将相同patient_id,不同concat的值合并为一行

合并相同 patient_id 的 JSON 数据为数组

要将相同 patient_id 的不同 JSON 数据合并为一行(合并为一个 JSON 数组),你可以使用 PostgreSQL 的 json_aggjsonb_agg 聚合函数。

解决方案(推荐)

sql 复制代码
SELECT 
    patient_id,
    json_agg(json_build_object('itemText', item_text, 'itemValue', item_value)) AS items
FROM 
    hs_patient_groups 
WHERE 
    active = 1
GROUP BY 
    patient_id;

结果示例

执行上述查询后,结果将类似于:

复制代码
patient_id                           | items
-------------------------------------+--------------------------------------------------------------------
eef41128c47c401abb7f8885a5f9fbdf    | [{"itemText":"旧","itemValue":"2"}, {"itemText":"新","itemValue":"1"}]
0564ccc4c07b4df98bf8207f7084b493    | [{"itemText":"患者1组","itemValue":"patientGroup1"}]
1801a4524b084d2b8197bfcc33849b24    | [{"itemText":"患者1","itemValue":"患者1"}, {"itemText":"患者2","itemValue":"患者2"}]
...                                  | ...

其他选项

  1. 如果你需要保持原始查询中的 JSON 字符串格式:
sql 复制代码
SELECT 
    patient_id,
    json_agg(concat('{"itemText":"',item_text,'","itemValue":"',item_value,'"}')::json) AS items
FROM 
    hs_patient_groups 
WHERE 
    active = 1
GROUP BY 
    patient_id;
  1. 如果你使用的是 PostgreSQL 9.4 或更早版本(没有 json_agg):
sql 复制代码
SELECT 
    patient_id,
    '[' || string_agg(concat('{"itemText":"',item_text,'","itemValue":"',item_value,'"}'), ',') || ']' AS items
FROM 
    hs_patient_groups 
WHERE 
    active = 1
GROUP BY 
    patient_id;

注意:最后一个方法生成的是 JSON 字符串而不是真正的 JSON 类型。

相关推荐
LYPHARD MELODY。1 小时前
将 JSON 批量转换为 XML:深度解析与完整实现指南
xml·json
爱吃涮毛肚的肥肥(暂时吃不了版)3 小时前
项目班——0510——JSON网络封装
c++·算法·json
GISer_Jing3 小时前
[前端高频]数组转树、数组扁平化、深拷贝、JSON.stringify&JSON.parse等手撕
前端·javascript·json
bang___bang_10 小时前
PostgreSQL内幕剖析——结构与架构
数据库·postgresql
小吕学编程15 小时前
Jackson使用详解
java·javascript·数据库·json
yuanpan15 小时前
MongoDB与PostgreSQL两个数据库的特点详细对比
数据库·mongodb·postgresql
双叶83617 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)(网页版预告)(html)(js)(json)
c语言·javascript·数据结构·html·json
zxrhhm1 天前
Oracle 中的虚拟列Virtual Columns和PostgreSQL Generated Columns生成列
postgresql·oracle·vr
电商数据girl1 天前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
懒大王爱吃狼2 天前
怎么使用python进行PostgreSQL 数据库连接?
数据库·python·postgresql