将3个表转换为1个JSON列postgres

时间:2017-11-05 08:02:30

标签: sql json postgresql

我设法做到了,但是一旦我不得不改变其中一个输入,我就无法让它发挥作用

这是有效的

array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])

这将返回一个包含3个表中的数据到1个json的列,并将其聚合在flow_id

现在我需要更改cerdentials表,以获得更复杂的东西

这将返回我需要的数据

select row_to_json(VVV) as json_flow
from (select *,(SELECT json_agg(row_to_json(
(SELECT t FROM (SELECT length, ip_src, pcap,num,coordinates,host,uri,user_agent,city_ids,ts,device,cookie) t))) AS cookies
FROM   public.dpkt_scan ck where flow.flow_id = ck.flow_id and ck.cookie IS NOT null
GROUP  BY flow_id),
(SELECT json_agg(row_to_json(
(SELECT b FROM (SELECT pnum, src_ip, pcap,file_name,file_extension,file_size, dst_ip, timestamp,md5) b))) AS files
FROM   public.files_scan f where flow.flow_id = f.flow_id
GROUP  BY flow_id),
(SELECT json_agg(row_to_json(
(SELECT c FROM (SELECT destination_ip, destination_port, pcap,packet_number,source_port,extraction, extraction_id, source_ip) c))) AS cerdentials
FROM   public.credentials_scan c where flow.flow_id = c.flow_id
GROUP  BY flow_id) from public.flows_updated flow) as VVV

现在我需要使用新查询来复制从cerdentials获取数据的部分...

我不是DBA,第一个查询是在我搜索了很多之后。 如果有人有更好的方法将3个表组成1个JSON列,并在字段上聚合,那么它也可以工作

总之,我无法取代 这部分(它选择从该表中取出哪些字段)

select cs_user.*, cs_user.flow_id, cs_user.pcap, cs_user.extraction as username, cs_pass.extraction as password
from credentials_scan cs_user left outer join credentials_scan cs_pass
on cs_user.packet_number=cs_pass.packet_number
and cs_user.pcap=cs_pass.pcap
where cs_user.extraction!= cs_pass.extraction
and cs_user.extraction like '%User%'
and cs_pass.extraction like '%Pass%'
group by cs_user.flow_id

使用新查询我展示了一些计算和逻辑

感谢。

编辑: 我的工作查询结果示例

SELECT destination_ip, destination_port, pcap,packet_number,source_port,extraction, extraction_id, source_ip

1 个答案:

答案 0 :(得分:0)

你可以只jsonb_agg结果,就像这里我加入两个表(你可以将你的逻辑放在CTE查询中)然后只显示结果为json:

so=# \pset format unaligned
Output format is unaligned.
so=# with joined as (select * from pg_database d join pg_stat_activity a on a.datname=d.datname where pid <> 98698)
select jsonb_pretty(jsonb_agg(joined)) from joined;
jsonb_pretty
[
    {
        "pid": 75073,
        "datid": "16661",
        "query": "select 0.15*14;",
        "state": "idle",
        "datacl": null,
        "datdba": "10",
        "datname": "so",
        "usename": "vao",
        "datctype": "UTF-8",
        "encoding": 6,
        "usesysid": "10",
        "datcollate": "C",
        "datminmxid": "1",
        "wait_event": null,
        "xact_start": null,
        "backend_xid": null,
        "client_addr": null,
        "client_port": -1,
        "query_start": "2017-10-31T22:31:42.236156+00:00",
        "backend_xmin": null,
        "datallowconn": true,
        "datconnlimit": -1,
        "datfrozenxid": "858",
        "state_change": "2017-10-31T22:31:42.242102+00:00",
        "backend_start": "2017-10-30T21:17:49.857856+00:00",
        "datistemplate": false,
        "datlastsysoid": "12668",
        "dattablespace": "1663",
        "client_hostname": null,
        "wait_event_type": null,
        "application_name": "psql"
    }
]
(1 row)
Time: 1.533 ms