[pgsql-jp: 41062] 再起的なSQLで配列の型について

ohara apostleofwhom @ gmail.com
2012年 3月 2日 (金) 00:11:15 JST


ohara です。

花田さん
どうもありがとうございました。

array_append とキャストでうまくいきました。
お手数おかけしました。


> path 列の型が varying[] 型でよければ、ARRAY[] や || を array_append() に
> 統一すれば OK のようです。
> 
> with recursive rec(id, parentid, lv, path) as (
>     select t.id, t.parentid, 1, array_append(NULL, t.id) path
>     from tree t
>     where t.parentid is null
>     union all
>     select t.id, t.parentid, rec.lv + 1, array_append(rec.path, t.id)
>     from tree t, rec
>     where t.parentid = rec.id
> )
> select id, parentid, lv, path
> from rec order by path;
> 
> もし path 列の型を varying(12)[] にしたい場合は、
>     array_append(rec.path, t.id)::varchar(12)[]
> のように array_append() の結果を UNION ALL の上下の両方キャストしてくだ
> さい。
> 

-- 
apostleofwhom <apostleofwhom @ gmail.com>



pgsql-jp メーリングリストの案内