Return parent 行然后在 Oracle SQL 查询中 child 行

Return parent row then child rows in Oracle SQL query

我正在尝试 return 来自 family 的数据,结构如下:

Family returns parent 和 child 数据一起,如:

到目前为止,我对 connect by prior 的尝试导致错误:

ORA-01436: CONNECT BY loop in user data

我怀疑,但我不确定,应该使用 connect by level 代替,但我的每次尝试都失败了。

我做错了什么?我怎样才能达到我想要的格式?

with parents
as (
    select *
    from view_sot sot
    where sot.tree = 'parent'
        and sot.xmode = 'E'
    ),
children
as (
    select *
    from view_sot sot
    where sot.tree = 'child'
        and sot.xmode != 'E'
    ),
family
as (
    select parents.srn psrn,
        parents.cand pcand,
        parents.ayrc payrc,
        parents.mod_code pmod_code,
        parents.mark pmark,
        parents.grade pgrade,
        parents.xmode pxmode,
        parents.mavo pmavo,
        parents.mod_name pmod_name,
        parents.prg_code pprg_code,
        parents.tree ptree,
        children.srn csrn,
        children.cand ccand,
        children.ayrc cayrc,
        children.mod_code cmod_code,
        children.mark cmark,
        children.grade cgrade,
        children.xmode cxmode,
        children.mavo cmavo,
        children.mod_name cmod_name,
        children.prg_code cprg_code,
        children.tree ctree
    from parents
    inner join children on parents.srn = children.srn
        and parents.mod_code = children.mod_code
        and parents.ayrc = children.ayrc
        and parents.cand = children.cand
        and parents.mavo = children.mavo
    where parents.srn = 'A012345678'
    )
select psrn,
    pxmode,
    pmod_code,
    cmod_code,
    cxmode,
    level
from family connect by prior cmod_code = pmod_code

非常感谢

在你的问题中没有任何上下文或数据,我只是构建了一个愚蠢的例子 - 这就是你想要的吗?

with family as
  (select 1 as id_num, 'Jay-Z' as the_name, 'parent' as who, 2 as family, 0 as parent_id from dual
    union all
   select 2 as id_num, 'Beyonce' as the_name, 'parent' as who, 2 as family,     0 as parent_id from dual
     union all
   select 11 as id_num, 'Blue Ivy' as the_name, 'child' as who, 2 as family,     1 as parent_id from dual
      union all
   select 11 as id_num, 'Blue Ivy' as the_name, 'child' as who, 2 as family,     2 as parent_id from dual
   union all
    select 22 as id_num, 'Upcoming Twin #1' as the_name, 'child' as who, 2     as family, 1 as parent_id from dual
   union all
    select 22 as id_num, 'Upcoming Twin #1' as the_name, 'child' as who, 2     as family, 2 as parent_id from dual
       union all
     select 33 as id_num, 'Upcoming Twin #2' as the_name, 'child' as who, 2     as family, 1 as parent_id from dual
   union all
    select 33 as id_num, 'Upcoming Twin #2' as the_name, 'child' as who, 2     as family, 2 as parent_id from dual)

select lpad('.',level*3-3,'.')||the_name
from family
where ((level = 1 and who = 'parent') or (level = 2))
connect by prior id_num = parent_id
order siblings by the_name;   



OUTPUT >>>>
Beyonce
...Blue Ivy
...Upcoming Twin #1
...Upcoming Twin #2
Jay-Z
...Blue Ivy
...Upcoming Twin #1
...Upcoming Twin #2

我不清楚每个 child 是否有多行(每个 child 每个 parent 一行)......但希望这种语法至少可以指导你.