select Fid,Fitemid
,Fparentid=isnull((select max(fid) from 表 where a.Fitemid like Fitemid+'.%'),0)
,Flevel=(select sum(1) from 表 where a.Fitemid like Fitemid+'%')
,ISLAST=case when exists(select 1 from 表 where Fitemid like a.Fitemid+'.%') then 0 else 1 end
from 表 a

解决方案 »

  1.   

    --下面是测试--测试数据
    declare @t table(Fid int,Fitemid varchar(20))
    insert into @t
    select 1,'01'
    union all select 2,'01.02'
    union all select 3,'01.02.01'
    union all select 4,'01.02.01.01'
    union all select 5,'01.02.01.01.01'
    union all select 6,'01.02.01.01.02'
    union all select 7,'01.02.01.01.03'
    union all select 8,'01.02.01.02'
    union all select 9,'01.02.01.03'
    union all select 10,'01.02.01.04'
    union all select 14,'01.03'
    union all select 15,'01.03.01'           
    union all select 16,'01.03.01.01'
    union all select 17,'01.03.01.01.01'--查询
    select Fid,Fitemid
    ,Fparentid=isnull((select max(fid) from @t where a.Fitemid like Fitemid+'.%'),0)
    ,Flevel=(select sum(1) from @t where a.Fitemid like Fitemid+'%')
    ,ISLAST=case when exists(select 1 from @t where Fitemid like a.Fitemid+'.%') then 0 else 1 end
    from @t a/*--测试结果(所影响的行数为 14 行)Fid         Fitemid              Fparentid   Flevel      ISLAST      
    ----------- -------------------- ----------- ----------- ----------- 
    1           01                   0           1           0
    2           01.02                1           2           0
    3           01.02.01             2           3           0
    4           01.02.01.01          3           4           0
    5           01.02.01.01.01       4           5           1
    6           01.02.01.01.02       4           5           1
    7           01.02.01.01.03       4           5           1
    8           01.02.01.02          3           4           1
    9           01.02.01.03          3           4           1
    10          01.02.01.04          3           4           1
    14          01.03                1           2           0
    15          01.03.01             14          3           0
    16          01.03.01.01          15          4           0
    17          01.03.01.01.01       16          5           1(所影响的行数为 14 行)
    --*/
      

  2.   

    测试:
    create table t1(fid int,fitemid varchar(20))
    insert t1 select  1,'01'
    union all select  2,'01.02'
    union all select 3,'01.02.01'
    union all select 4,'01.02.01.01'
    union all select 5,'01.02.01.01.01'
    union all select 6,'01.02.01.01.02'
    union all select 7,'01.02.01.01.03'
    union all select        8,'01.02.01.02'                
    union all select 9,'01.02.01.03'    
    union all select 10,'01.02.01.04'    
    union all select        14,'01.03'    
    union all select 15,'01.03.01'           
    union all select 16,'01.03.01.01'    
    union all select 17,'01.03.01.01.01'select a.fid,a.fitemid,
    (case when len(fitemid)<=3 then 0 else (select fid from t1 where fitemid = left(a.fitemid,len(a.fitemid)-3) and len(a.fitemid)>3) end)
    as fparentid
    ,(len(a.fitemid)-3)/2 +1as flevel
    ,(case when exists(select 1 from t1 where len(fitemid) > len(a.fitemid) and  left(fitemid,len(a.fitemid)) = a.fitemid) then 0 else 1 end)
    from t1 a fid         fitemid              fparentid   flevel                  
    ----------- -------------------- ----------- ----------- ----------- 
    1           01                   0           1           0
    2           01.02                1           2           0
    3           01.02.01             2           3           0
    4           01.02.01.01          3           5           0
    5           01.02.01.01.01       4           6           1
    6           01.02.01.01.02       4           6           1
    7           01.02.01.01.03       4           6           1
    8           01.02.01.02          3           5           1
    9           01.02.01.03          3           5           1
    10          01.02.01.04          3           5           1
    14          01.03                1           2           0
    15          01.03.01             14          3           0
    16          01.03.01.01          15          5           0
    17          01.03.01.01.01       16          6           1(所影响的行数为 14 行)
      

  3.   

    select a.*
           (case when a.fid=b.fid then 0 else b.fid end) as fparentid,
           (len(a.fitemid)+1)/3 as flevel,
           (case when c.fitemid is null then 1 else 0 end) as islast
    from 
         (  select *,
                   ( case when len(fitemid)=2 then fitemid
                          else left(fitemid,len(fitemid)-3)
                     end 
                   ) as pitemid
            from tablename
         ) a 
         left join tablename b on a.pitemid=b.fitemid
         left join 
         ( select distinct left(fitemid,len(fitemid)-3) as fitemid
           from tablename 
           where len(fitemid)>2
         ) c 
         on a.fitemid=c.fitemid