首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PosgreSQL 13.4 json聚合辅助

PosgreSQL 13.4 json聚合辅助
EN

Stack Overflow用户
提问于 2022-04-29 20:40:24
回答 1查看 25关注 0票数 0

我正在尝试将多个SQL行合并成一个大型的JSON数组。到目前为止,我有一个查询.

代码语言:javascript
复制
SELECT asset_id, jsonb_build_object('vender',vendor,'family',family,'name',name,'version',version,'type',type,'cpe',cpe) as software 
from dim_asset_software 
where asset_id = '1214';

我得到这个输出..。

代码语言:javascript
复制
     asset_id |                                                                                software                                                                                
----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     1214 | {"cpe": null, "name": "Wireshark", "type": "<unknown>", "family": null, "vender": "Wireshark", "version": "2.6.3"}
     1214 | {"cpe": null, "name": "WinPcap 4.1.3", "type": "<unknown>", "family": null, "vender": "Riverbed Technology, Inc.", "version": "4.1.0.2980"}
     1214 | {"cpe": null, "name": ".NET Framework 4.6.2 Client Profile", "type": "<unknown>", "family": ".NET Framework", "vender": "Microsoft", "version": null}
     1214 | {"cpe": null, "name": "Internet Information Services", "type": "Internet Server", "family": "Internet Information Services", "vender": "Microsoft", "version": "10.0"}
     1214 | {"cpe": null, "name": ".NET Framework 4.6.2", "type": "<unknown>", "family": ".NET Framework", "vender": "Microsoft", "version": null}
     1214 | {"cpe": null, "name": "Windows Media Player", "type": "Media Client", "family": "Windows Media Player", "vender": "Microsoft", "version": "12.0.10011.16384"}
     1214 | {"cpe": null, "name": "Internet Explorer", "type": "Internet Client", "family": "Internet Explorer", "vender": "Microsoft", "version": "11.4350.14393.0"}
     1214 | {"cpe": null, "name": "MSXML", "type": "<unknown>", "family": "MSXML", "vender": "Microsoft", "version": "8.110.14393.4704"}
     1214 | {"cpe": null, "name": "Rapid7 InsightVM Scan Assistant v", "type": "<unknown>", "family": null, "vender": "Rapid7", "version": "1.0.0"}
     1214 | {"cpe": null, "name": "MSXML", "type": "<unknown>", "family": "MSXML", "vender": "Microsoft", "version": "6.30.14393.5006"}
     1214 | {"cpe": null, "name": "HTML Document", "type": "Browser Add-On", "family": null, "vender": null, "version": "11.0.14393.5066"}

如果可能的话,我试图将所有的软件信息放到一个嵌套的json行中。示例:

代码语言:javascript
复制
  asset_id |                                                                                software                                                                                
----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     1214 | [{"cpe": null, "name": "Wireshark", "type": "<unknown>", "family": null, "vender": "Wireshark", "version": "2.6.3"}, {"cpe": null, "name": "WinPcap 4.1.3", "type": "<unknown>", "family": null, "vender": "Riverbed Technology, Inc.", "version": "4.1.0.2980"}, {"cpe": null, "name": ".NET Framework 4.6.2 Client Profile", "type": "<unknown>", "family": ".NET Framework", "vender": "Microsoft", "version": null}, {"cpe": null, "name": "Internet Information Services", "type": "Internet Server", "family": "Internet Information Services", "vender": "Microsoft", "version": "10.0"}, {"cpe": null, "name": ".NET Framework 4.6.2", "type": "<unknown>", "family": ".NET Framework", "vender": "Microsoft", "version": null}, {"cpe": null, "name": "Windows Media Player", "type": "Media Client", "family": "Windows Media Player", "vender": "Microsoft", "version": "12.0.10011.16384"}, {"cpe": null, "name": "Internet Explorer", "type": "Internet Client", "family": "Internet Explorer", "vender": "Microsoft", "version": "11.4350.14393.0"}, {"cpe": null, "name": "MSXML", "type": "<unknown>", "family": "MSXML", "vender": "Microsoft", "version": "8.110.14393.4704"}, {"cpe": null, "name": "Rapid7 InsightVM Scan Assistant v", "type": "<unknown>", "family": null, "vender": "Rapid7", "version": "1.0.0"}, {"cpe": null, "name": "MSXML", "type": "<unknown>", "family": "MSXML", "vender": "Microsoft", "version": "6.30.14393.5006"}, {"cpe": null, "name": "HTML Document", "type": "Browser Add-On", "family": null, "vender": null, "version": "11.0.14393.5066"}]

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-04-30 02:01:25

这似乎起作用了。如果你看到问题请告诉我。谢谢。

代码语言:javascript
复制
SELECT asset_id, json_agg(temp_software) AS software
from dim_asset_software, jsonb_build_object('vender',vendor,'family',family,'name',name,'version',version,'type',type,'cpe',cpe) as temp_software
where asset_id = '1214'
GROUP by asset_id;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72063470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档