SQL SYS_CONNECT_BY_PATH指南

时间:2015-05-13 19:26:39

标签: sql oracle recursion hierarchical-data

你好新的董事会,并一直在浏览类似的主题,但无法完善一个适用于我的用例的查询,如果有人在这里请帮助我,我将不胜感激!我一直在玩下面的查询,如果我包含一个儿童ID,它似乎正在工作,但我想得到一个根据个人资料ID = 2的任何东西的ROOTS列表,而不必由每个孩子单独做。我尝试从Start With行中删除CHILD_ID,但后来我获得了所有配置文件的值。另外,正如您所知,每个配置文件都有多个父母

select distinct lpad(' ',2*(level - 1)) || child_id, SYS_CONNECT_BY_PATH(description, '>') ROOT
from mastertable
start with child_id = '1' and profile_id = '2'
connect by nocycle prior child_id = parent_id
PROFILE_ID  CHILD_ID    PARENT_ID   DESCRIPTION
2           2           0           Mailing Address
2           1           0           Legal Address
2           7           1           Address 1
2           8           1           Address 2
2           9           1           Address 3
2           10          1           Address 4
2           11          1           City
2           12          1           State
2           13          1           Country
2           14          1           Zip / Postal Code
2           15          14          Zip / Postal Code
2           16          14          Zip Code
2           17          14          Zip / Postal Code
2           18          1           Special Instructions
3           20          0           Mailing Address
3           21          0           Legal Address
3           22          21          City
3           23          21          State
3           24          21          Country
3           25          21          Zip / Postal Code
3           26          25          Zip / Postal Code
3           27          25          Zip Code
3           28          25          Zip / Postal Code
3           29          21          Special Instructions

1 个答案:

答案 0 :(得分:0)

从那些没有父母的记录开始:

[ `echo $* | grep $SearchWord| wc -l` -eq 1 ] && echo "Present"|| echo "Not present"

@Ponder Stibbons,感谢SQL小提琴! Modified query.