【问题标题】:replace elements in a 2d array替换二维数组中的元素
【发布时间】:2018-11-19 19:41:05
【问题描述】:

给定一个二维数组

select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]);

{{1,2,3},{4,0,0},{7,8,9}}

有没有办法用值 5 和 6 替换 [2:2][2:]{{0,0}})处的切片? array_replace 替换了一个特定的值,所以我不确定如何处理。

【问题讨论】:

    标签: postgresql postgresql-11


    【解决方案1】:

    我相信在 plpgsql 中编写函数代码更具可读性。但是,也存在纯 SQL 解决方案: select ( select array_agg(inner_array order by outer_index) from ( select outer_index, array_agg( case when outer_index = 2 and inner_index = 2 then 5 when outer_index = 2 and inner_index = 3 then 6 else item end order by inner_index ) inner_array from ( select item, 1 + (n - 1) % array_length(a, 1) inner_index, 1 + (n - 1) / array_length(a, 2) outer_index from unnest(a) with ordinality x (item, n) ) _ group by outer_index )_ ) from ( select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]) a )_;

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多