如何获取所有出现的分层数据?
How do I get all occurrences of hierarchical data?
在我的 PostgreSQL 数据库 (version: 11.4
) 中,我有 table 调用 organizations
。这个 table 有这样的结构:
| organization_id | organization_name | parent_organization_id | tree_organization_id | organization_rang |
|-----------------|-------------------|------------------------|----------------------|-------------------|
| 1 | Alphabet | | | 1 |
| 2 | Google | 1 | | 2 |
| 3 | Calico | 1 | | 2 |
| 4 | Youtube | 2 | | 3 |
| 5 | Nest | 2 | | 3 |
| 6 | Facebook | | | 1 |
| 7 | Whatsapp | 5 | | 2 |
| 8 | Instagram | 5 | | 2 |
如您所见,此 table 存储有关组织之间层次关系的信息。假设作为输入,我有特定的 organization_id
值。例如,它可以是 4 (Youtube)。我需要创建一个名为 dependencies
的新列,其中将存储所有具有组织 ID 的 tree_organization_id
。这个新列的数据类型必须是 string array []text
.
换句话说,我正在尝试获得类似的结果:
| organization_id | organization_name | parent_organization_id | tree_organization_id | organization_rang | dependencies |
|-----------------|-------------------|------------------------|----------------------|-------------------|---------------------------|
| 1 | Alphabet | | | 1 | ['', '', '' ] |
| 2 | Google | 1 | | 2 | ['', ''] |
| 4 | Youtube | 2 | | 3 | [''] |
我使用了这样的 SQL 请求来找到组织的父级,但是如何正确计算 dependencies
列中的值?
with recursive hierarchy as (
select
organizations.organization_id,
organizations.organization_name,
organizations.parent_organization_id,
organizations.tree_organization_id,
organizations.organization_rang
from
organizations
and
organizations.organization_id in (4)
union
select
a.organization_id,
a.organization_name,
a.organization_rang,
a.parent_organization_id,
a.tree_organization_id
from
organizations a
inner join
hierarchy b
on
a.organization_id = b.parent_organization_id
)
select
hierarchy.organization_id,
hierarchy.organization_name,
hierarchy.parent_organization_id,
hierarchy.tree_organization_id,
hierarchy.organization_rang
from
hierarchy
order by
hierarchy.organization_rang;
如果我没理解错的话,你不需要递归 CTE。您可以只匹配 tree
列:
select o.*,
(select array_agg(o2.tree order by o2.rang)
from organizations o2
where o2.tree like (o.tree || '%')
) as dependencies
from organizations o;
Here 是一个 db<>fiddle.
请注意,我将反斜杠更改为正斜杠。反斜杠是字符串中的转义字符,因此使用起来很挑剔。
在我的 PostgreSQL 数据库 (version: 11.4
) 中,我有 table 调用 organizations
。这个 table 有这样的结构:
| organization_id | organization_name | parent_organization_id | tree_organization_id | organization_rang |
|-----------------|-------------------|------------------------|----------------------|-------------------|
| 1 | Alphabet | | | 1 |
| 2 | Google | 1 | | 2 |
| 3 | Calico | 1 | | 2 |
| 4 | Youtube | 2 | | 3 |
| 5 | Nest | 2 | | 3 |
| 6 | Facebook | | | 1 |
| 7 | Whatsapp | 5 | | 2 |
| 8 | Instagram | 5 | | 2 |
如您所见,此 table 存储有关组织之间层次关系的信息。假设作为输入,我有特定的 organization_id
值。例如,它可以是 4 (Youtube)。我需要创建一个名为 dependencies
的新列,其中将存储所有具有组织 ID 的 tree_organization_id
。这个新列的数据类型必须是 string array []text
.
换句话说,我正在尝试获得类似的结果:
| organization_id | organization_name | parent_organization_id | tree_organization_id | organization_rang | dependencies |
|-----------------|-------------------|------------------------|----------------------|-------------------|---------------------------|
| 1 | Alphabet | | | 1 | ['', '', '' ] |
| 2 | Google | 1 | | 2 | ['', ''] |
| 4 | Youtube | 2 | | 3 | [''] |
我使用了这样的 SQL 请求来找到组织的父级,但是如何正确计算 dependencies
列中的值?
with recursive hierarchy as (
select
organizations.organization_id,
organizations.organization_name,
organizations.parent_organization_id,
organizations.tree_organization_id,
organizations.organization_rang
from
organizations
and
organizations.organization_id in (4)
union
select
a.organization_id,
a.organization_name,
a.organization_rang,
a.parent_organization_id,
a.tree_organization_id
from
organizations a
inner join
hierarchy b
on
a.organization_id = b.parent_organization_id
)
select
hierarchy.organization_id,
hierarchy.organization_name,
hierarchy.parent_organization_id,
hierarchy.tree_organization_id,
hierarchy.organization_rang
from
hierarchy
order by
hierarchy.organization_rang;
如果我没理解错的话,你不需要递归 CTE。您可以只匹配 tree
列:
select o.*,
(select array_agg(o2.tree order by o2.rang)
from organizations o2
where o2.tree like (o.tree || '%')
) as dependencies
from organizations o;
Here 是一个 db<>fiddle.
请注意,我将反斜杠更改为正斜杠。反斜杠是字符串中的转义字符,因此使用起来很挑剔。