代码之家  ›  专栏  ›  技术社区  ›  Evan Zamir

在Postgres中重塑数组?

  •  0
  • Evan Zamir  · 技术社区  · 5 年前

    我在Postgres中有一个一维值数组,例如:

    [ "1", "4", "2", "5", "3", "6" ]
    

    [ ["1", "4"], ["2", "5"], ["3", "6"] ]
    

    现在尺寸是3x2而不是1x6。

    1 回复  |  直到 5 年前
        1
  •  1
  •   klin    5 年前

    使用两种聚合:

    select array_agg(arr order by ord)
    from (
        select (ord+ 1) / 2 as ord, array_agg(arr) as arr
        from unnest(array[1,4,2,5,3,6]) with ordinality as u(arr, ord)
        group by 1
        ) s
    

    Db<>fiddle.