楼主去看看树形查询,start with...connect by ...

解决方案 »

  1.   


    with t1 as
     (select 1 id, 'A' name, 0 r_id, '班长' role
        from dual
      union all
      select 2 id, 'B' name, 1 r_id, '组长' role
        from dual
      union all
      select 3 id, 'C' name, 1 r_id, '组长' role
        from dual
      union all
      select 4 id, 'D' name, 2 r_id, '组员' role
        from dual
      union all
      select 5 id, 'E' name, 0 r_id, '班长' role
        from dual
      union all
      select 6 id, 'F' name, 5 r_id, '组长' role
        from dual
      union all
      select 7 id, 'G' name, 6 r_id, '组员' role
        from dual
      union all
      select 8 id, 'H' name, 6 r_id, '组员' role
        from dual
      union all
      select 9 id, 'I' name, 0 r_id, '班长' role from dual)
    select (select name from t1 where t1.id = t5.b_id) 班长,
           (select name from t1 where t1.id = t5.z_id) 组长,
           (select name from t1 where t1.id = t5.id) 组员
      from (select (select r_id from t1 where t1.id = t3.z_id) b_id, t3.*
              from (select (select id from t1 where t1.id = t2.r_id) z_id, t2.id
                      from t1 t2
                     where t2.role = '组员') t3
            union
            select r_id b_id, id z_id, null
              from t1
             where t1.role = '组长'
               and not exists (select 1 from t1 t2 where t1.id = t2.r_id)
            union
            select id b_id, null, null
              from t1
             where t1.role = '班长'
               and not exists (select 1 from t1 t2 where t1.id = t2.r_id)) t5;
      

  2.   

    是这样的
    请参考oca认证里面,有这样的问题解答!
      

  3.   


    是这样的
    请参考oca认证里面,有这样的问题解答!