親IDを持つこれらを、親のnameから順に左から並んでいるテーブルを作成したいです
より子供の方からleftjoinでparent_idを紐付けてくっつけていくと、親の階層が揃いません。
親の方から数えると、aとgは同じ階層になるはずですが、親をひたすらたどるクエリを書くと、階層数が異なるため、崩れた表になってしまいます。どのように書いたらいいでしょうか
id | parent_id | name |
---|---|---|
1 | null | a |
2 | 3 | b |
3 | 4 | c |
4 | 1 | d |
5 | null | e |
6 | 7 | f |
7 | null | g |
8 | 6 | h |
9 | null | i |
10 | null | j |
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
こういった自己結合によるツリー形式のテーブル(ナイーブツリー)にはwith recursive
による再帰読出を使用すると、階層の深さなどを条件として意識する必要がありません。
但し、自己結合の関係(今回であれば、idとparent_id)がループするデータがあると無限ループします。
階層の深さで制限を掛けるなどしてデータの確認を行って下さい。
※サンプルでは階層のレベル(=lvl)も付加してあります。
データ
SQL
1drop table if exists list; 2create table list (id int, parent_id int, name text); 3insert into list values 4 (1,null,'a') 5,(2,3,'b') 6,(3,4,'c') 7,(4,1,'d') 8,(5,Null,'e') 9,(6,7,'f') 10,(7,null,'g') 11,(8,6,'h') 12,(9,null,'i') 13,(10,null,'j') 14;
問い合わせ(Postgres)
SQL
1with recursive tree as ( 2 select * 3 , 1 ::int as lvl 4 , array[name] as lvl_name 5 from list 6 where parent_id is null 7 union all 8 select list.* 9 , tree.lvl+1 10 , array_append(tree.lvl_name, list.name) 11 from list inner join tree on list.parent_id=tree.id 12) 13select id, parent_id, name 14 , lvl_name[1] as name1 15 , lvl_name[2] as name2 16 , lvl_name[3] as name3 17 , lvl_name[4] as name4 18from tree 19-- where lvl<=4 20order by lvl_name
追記 問い合わせ(Hive)
親を辿る方向で外部結合し、その状況から何階層目かを求めます(ここポイント)
その位置情報から取得すべき階層を判定し求めます。
※元データにこの何階層目かを判断できる情報があれば、その分簡潔になります。
SQL
1select id, parent_id, name 2 , case lvl when 4 then name_nest4 when 3 then name_nest3 when 2 then name_nest2 when 1 then name_nest1 end as name1 3 , case lvl when 4 then name_nest3 when 3 then name_nest2 when 2 then name_nest1 end as name2 4 , case lvl when 4 then name_nest2 when 3 then name_nest1 end as name3 5 , case lvl when 4 then name_nest1 end as name4 6from ( 7 select t1.* 8 , t1.name as name_nest1 9 , t2.name as name_nest2 10 , t3.name as name_nest3 11 , t4.name as name_nest4 12 , case when t4.id is not null then 4 13 else case when t3.id is not null then 3 14 else case when t2.id is not null then 2 else 1 end 15 end end as lvl 16 from list t1 left join list t2 on t2.id=t1.parent_id 17 left join list t3 on t3.id=t2.parent_id 18 left join list t4 on t4.id=t3.parent_id 19) step1
投稿2017/10/06 01:32
編集2017/10/06 09:10総合スコア25206
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。