繁体   English   中英

从 JSON 和 PostgreSQL 提取到多列

[英]Extract into multiple columns from JSON with PostgreSQL

我有一列item_id包含 JSON (像?)结构中的数据。

+----------+---------------------------------------------------------------------------------------------------------------------------------------+
|     id   |                                                                item_id                                                                |
+----------+---------------------------------------------------------------------------------------------------------------------------------------+
|    56711 | {itemID":["0530#2#1974","0538\/2#2#1974","0538\/3#2#1974","0538\/18#2#1974","0539#2#1974"]}"                                          |
|    56712 | {itemID":["0138528#2#4221","0138529#2#4221","0138530#2#4221","0138539#2#4221","0118623\/2#2#4220"]}"                                  |
|    56721 | {itemID":["2704\/1#1#1356"]}"                                                                                                         |
|    56722 | {itemID":["0825\/2#2#3349","0840#2#3349","0844\/10#2#3349","0844\/11#2#3349","0844\/13#2#3349","0844\/14#2#3349","0844\/15#2#3349"]}" |
|    57638 | {itemID":["0161\/1#2#3364","0162\/1#2#3364","0163\/2#2#3364"]}"                                                                       |
|    57638 | {itemID":["109#1#3364","110\/1#1#3364"]}"                                                                                             |
+----------+---------------------------------------------------------------------------------------------------------------------------------------+

我需要每个逗号之前的最后四位数字(如果有),最后 4 位数字被区分并分成单独的列。
distinct 也应该发生在id上,因此只允许一个 id: 57638 的结果行。

这是一个没有给出正确答案的代码草案。 所需的结果应如下所示:

+----------+-----------+-----------+
|    id    | item_id_1 | item_id_2 |
+----------+-----------+-----------+
|    56711 |      1974 |           |
|    56712 |      4220 |      4221 |
|    56721 |      1356 |           |
|    56722 |      3349 |           |
|    57638 |      3364 |      3365 |
+----------+-----------+-----------+

结果中可能有很多“item_id_%”列。

您可以取消嵌套 json 数组,获取每个元素的最后 4 个字符作为数字,然后进行条件聚合:

select 
    id,
    max(val) filter(where rn = 1) item_id_1,
    max(val) filter(where rn = 2) item_id_2
from (
    select
        id,
        right(val, 4)::int val,
        dense_rank() over(partition by id order by right(val, 4)::int) rn
    from mytable t
    cross join lateral jsonb_array_elements_text(t.item_id -> 'itemID') as x(val)
) t
group by id

您可以向外部查询添加更多条件max()以处理更多可能的值。

DB Fiddle 上的演示

   id | item_id_1 | item_id_1
----: | --------: | --------:
56711 |      1974 |      null
56712 |      4220 |      4221
56721 |      1356 |      null
56722 |      3349 |      null
57638 |      3364 |      3365
with the_table (id, item_id) as (
values
(56711, '{"itemID":["0530#2#1974","0538\/2#2#1974","0538\/3#2#1974","0538\/18#2#1974","0539#2#1974"]}'),
(56712, '{"itemID":["0138528#2#4221","0138529#2#4221","0138530#2#4221","0138539#2#4221","0118623\/2#2#4220"]}'),
(56721, '{"itemID":["2704\/1#1#1356"]}'),
(56722, '{"itemID":["0825\/2#2#3349","0840#2#3349","0844\/10#2#3349","0844\/11#2#3349","0844\/13#2#3349","0844\/14#2#3349","0844\/15#2#3349"]}'),
(57638, '{"itemID":["0161\/1#2#3364","0162\/1#2#3364","0163\/2#2#3364"]}'),
(57638, '{"itemID":["109#1#3365","110\/1#1#3365"]}')
)
select id
    ,(array_agg(itemid)) [1] itemid_1
    ,(array_agg(itemid)) [2] itemid_2
from (
    select distinct id
        ,split_part(replace(json_array_elements(item_id::json -> 'itemID')::text, '"', ''), '#', 3)::int itemid
    from the_table
    order by 1
        ,2
    ) t
group by id

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM