仅保留第一列的第一个值并根据层次结构对其他两列进行排序
Only keep first value of first column and sort the two other columns according to hierarchy
所以有五个人在 Laugh 工厂工作。 Marc 先上来,然后是 Judy,然后是 Lorie,然后是 Agnes,最后是 Isaiah。
我想从这里开始:
+------------------------------+
| Laugh Factory |Marc | Judy |
| Laugh Factory |Judy | Lorie |
| Laugh Factory |Lorie| Agnes |
| Laugh Factory |Agnes| Isaiah |
+------------------------------+
为此:
Laugh Factory | Marc | Judy | Lorie | Agnes | Isaiah
我该如何实现?
为此你需要 recursive query。
沿线的东西(未经测试):
with recursive tree as (
select company, comedian, preceding_comedian, 1 as level
from the_table
where company = 'Laugh Factory'
and preceding_comedian is null
union all
select ch.company, ch.comedian, ch.preceding_comedian, p.level + 1
from the_table ch
join tree p on ch.preceding_comedian = p.comedian
)
select company, string_agg(comedian, ' > ' order by level) as comedians
from tree
group by company;
所以有五个人在 Laugh 工厂工作。 Marc 先上来,然后是 Judy,然后是 Lorie,然后是 Agnes,最后是 Isaiah。
我想从这里开始:
+------------------------------+
| Laugh Factory |Marc | Judy |
| Laugh Factory |Judy | Lorie |
| Laugh Factory |Lorie| Agnes |
| Laugh Factory |Agnes| Isaiah |
+------------------------------+
为此:
Laugh Factory | Marc | Judy | Lorie | Agnes | Isaiah
我该如何实现?
为此你需要 recursive query。
沿线的东西(未经测试):
with recursive tree as (
select company, comedian, preceding_comedian, 1 as level
from the_table
where company = 'Laugh Factory'
and preceding_comedian is null
union all
select ch.company, ch.comedian, ch.preceding_comedian, p.level + 1
from the_table ch
join tree p on ch.preceding_comedian = p.comedian
)
select company, string_agg(comedian, ' > ' order by level) as comedians
from tree
group by company;