层次结构表将所有连接级别打开为行 - 雪花

问题描述 投票:0回答:1

我有一个层次结构表,其中包含实体之间的连接。 例如:

实体_id 实体类型 资源_id 资源类型
1 X 2
2 3
3 4 Z

我最终想要得到什么:

实体_id 实体类型 资源_id 资源类型
1 X 2
2 3
3 4 Z
1 X 3
1 4 Z
2 4 Z

我尝试通过多重连接来连接自身(我知道我最多有 5 层深度)。 但它只带来最终的连接(例如1-4)而不是之间的连接(2-4、1-3)

snowflake-cloud-data-platform hierarchical-data recursive-query
1个回答
0
投票

你必须使用递归cte才能得到想要的结果。找到下面的sql片段

with cte as(
    select entity_id,entity_type,resource_id,resource_type from q1
    union all
    select cte.entity_id,cte.entity_type,q1.resource_id,q1.resource_type 
    from cte
    inner join q1 
    on cte.resource_id=q1.entity_id
)
select * from cte;
© www.soinside.com 2019 - 2024. All rights reserved.