【问题标题】:Aggregate concatenate jsonb arrays聚合连接 jsonb 数组
【发布时间】:2019-06-24 16:49:59
【问题描述】:

有一个名为 example_table 的表,其中有一列名为 example_column 的 JSONB 类型,并且该列中的每个值都是一个数组。

将 2 行中的值设为:[1, 2] 和 [3]

如何聚合-连接example_column 中的值?

结果应该是:[1,2,3]

我尝试使用:

select json_agg(example_column) from example_table

但返回[[1, 2,], [3]]

【问题讨论】:

    标签: postgresql jsonb


    【解决方案1】:

    使用函数jsonb_array_elements(example_column),例子:

    with example_table(example_column) as (
    values
        (jsonb '[1, 2]'),
        (jsonb '[3]')
    )
    
    select jsonb_agg(value)
    from example_table
    cross join jsonb_array_elements(example_column) 
    
    jsonb_agg 
    -----------
     [1, 2, 3]
    (1 row)
    

    您可以定义聚合元素的排序顺序和/或删除重复项,例如:

    with example_table(id, example_column) as (
    values
        (1, jsonb '[1, 2]'),
        (2, jsonb '[3]'),
        (3, jsonb '[3, 1]')
    )
    
    select 
        jsonb_agg(value order by id) as agg1,
        jsonb_agg(value order by value) as agg2,
        jsonb_agg(distinct value order by value) as agg3
    from example_table
    cross join jsonb_array_elements(example_column) 
    
          agg1       |      agg2       |   agg3    
    -----------------+-----------------+-----------
     [1, 2, 3, 3, 1] | [1, 1, 2, 3, 3] | [1, 2, 3]
    (1 row)
    

    【讨论】:

    • 它实际上返回 [3, 1, 2] 而不是 [1, 2, 3] 但对于我的用例它可以满足我的需要
    【解决方案2】:

    如果您需要做很多事情,您可以为此创建自己的聚合:

    create function combine_jsonb_arrays(p_array_1 jsonb, p_array_2 jsonb) 
      returns jsonb
    as
    $$
      select jsonb_agg(t.val order by t.val)
      from (
        select *
        from jsonb_array_elements(p_array_1) as x1(val)
        union all
        select *
        from jsonb_array_elements(p_array_2) as x2(val)
      ) t;
    $$
    language sql;
    
    create aggregate jsonb_elements_agg(jsonb)
    (
      sfunc = combine_jsonb_arrays,
      stype = jsonb
    );
    

    那么你可以这样使用它:

    select jsonb_elements_agg(example_column)
    from example_table;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2019-12-31
      • 2021-11-03
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多