返回父行,然后在Oracle SQL查询中返回子行

时间:2017-03-28 16:57:24

标签: sql oracle hierarchical-data hierarchical

我试图在以下结构中从family返回数据:

  • 家长一行
  • A的孩子1排
  • A的孩子2排
  • 一个孩子3排
  • 父B行
  • B&#39的孩子1排
  • B的孩子2排
  • B' 3行

Family一起返回父数据和子数据,例如:

  • 1个父A列,A的孩子1列
  • 2个父母A列,A的孩子2列
  • 3位父母A列,A的孩子3列

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

  

ORA-01436:用户数据中的CONNECT BY循环

我怀疑,但我不确定,应该使用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

非常感谢

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题