表T(ID ,FID,SXH)
数据(1,0,1
      2,0,2
      3,1,1
      4,1,2
      5,1,3
      6,2,1
      7,2,2)
fid是ID的父ID,SXH是本ID在父ID下的顺序号
我想通过SQL语句得到
ID,SXH和其父ID的sxh(没有父ID的记录可以忽略或返回空,或零)
即 id ,fid,sxh,f_sxh
(    1,0,1,0
      2,0,2,0
      3,1,1,1
      4,1,2,1
      5,1,3,1
      6,2,1,2
      7,2,2,2)
       

解决方案 »

  1.   

    是不是只需把父亲的id找出来就行了?爷爷的id不要吧?
    另外,f_sxh是什么含义呢?父亲在爷爷中的排位?select t1.id , t1.fid, t1.sxh, t2.sxh
    from t t1, t t2
    where t1.fid = t2.id这样行不?
      

  2.   

    记录中已经有父ID的号
    比如记录4,1,2 其父ID 是1 ,1的顺序号为1结果应该为
    4,1,2,1
    我现在就想通过SQL语句得到这样的结果
      

  3.   

    记录中已经有父ID的号
    比如记录4,1,2 其父ID 是1 ,1的顺序号为1结果应该为
    4,1,2,1
    我现在就想通过SQL语句得到这样的结果
      

  4.   


    with tmp as
    (
    select 1 id,0 fid,1 sxh from dual union all
    select   2 id,0 fid,2 sxh from dual union all
    select   3 id,1 fid,1 sxh from dual union all
    select   4 id,1 fid,2 sxh from dual union all
    select   5 id,1 fid,3 sxh from dual union all
    select   6 id,2 fid,1 sxh from dual union all
    select   7 id,2 fid,2 sxh from dual)
    select mt.id, mt.fid, mt.sxh, nvl(st.sxh,0) f_sxh
    from tmp mt, tmp st
    where mt.fid = st.id(+)
    order by mt.id;        ID        FID        SXH      F_SXH
    ---------- ---------- ---------- ----------
             1          0          1          0
             2          0          2          0
             3          1          1          1
             4          1          2          1
             5          1          3          1
             6          2          1          2
             7          2          2          2