
イメージ的に似てるのは ↓
R version 3.2.4 Revised (2016-03-16 r70336) -- "Very Secure Dishes" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-unknown-cygwin (64-bit) ... > matrix(1:100, nrow=10) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100

簡単な例で、テキスト
select '{あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道,土,間,土,間,土,~,間}' :: text[]; +---------------------------------------------------------------------+ | text | +---------------------------------------------------------------------+ | {あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道,土,間,土,間,土,~,間} | +---------------------------------------------------------------------+ (1 row)
まず、指定した行サイズごとに別々の
with a (nrow, ncol, ary) as ( select 5, 4,-- 任意の行・列サイズ '{あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道,土,間,土,間,土,~,間}' :: text[] ) select i,ary[low : low + ncol - 1] from a, generate_series(1, nrow) as i, cast((i - 1) * ncol + 1 as int) as low; +---+---------------+ | i | ary | +---+---------------+ | 1 | {あ,る,晴,れ} | | 2 | {た,昼,下,が} | | 3 | {り,市,場,へ} | | 4 | {続,く,道,土} | | 5 | {間,土,間,土} | +---+---------------+ (5 rows)

後は
with a (nrow, ncol, ary) as ( select 5, 4, '{あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道,土,間,土,間,土,~,間}' :: text[] ) selectarray_agg(ary[low : low + ncol - 1]) from a, generate_series(1, nrow) as i, cast((i - 1) * ncol + 1 as int) as low; +-------------------------------------------------------------------------+ | array_agg | +-------------------------------------------------------------------------+ | {{あ,る,晴,れ},{た,昼,下,が},{り,市,場,へ},{続,く,道,土},{間,土,間,土}} | +-------------------------------------------------------------------------+ (1 row)

1
with a (nrow, ncol, ary) as ( select 5, 4, '{あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道}' :: text[] ) select array_agg(ary[low : low + ncol - 1]) from a, generate_series(1, nrow) as i, cast((i - 1) * ncol + 1 as int) as low;ERROR: cannot accumulate arrays of different dimensionality
最後に、1
with a (nrow, ncol, ary) as ( select 5, 4, '{あ,る,晴,れ,た,昼,下,が,り,市,場,へ,続,く,道,土,間,土,間,土,~,間}' :: text[] ), b (ary_rev) as ( select array_agg(val order by o desc)-- 反転した配列 from a, unnest(ary) with ordinality as x(val, o) ) select array_agg(ary_rev[low : low + ncol - 1] ) from a, b, generate_series(1, nrow) as i, cast((i - 1) * ncol + 1 as int) as low; +-------------------------------------------------------------------------+ | array_agg | +-------------------------------------------------------------------------+ | {{間,~,土,間},{土,間,土,道},{く,続,へ,場},{市,り,が,下},{昼,た,れ,晴}} | +-------------------------------------------------------------------------+ (1 row)
ところで