有一个自调用表 table1ID, ManagerID, Title
1,  NULL,      CEO
2,  1,         CTO
3,  2,         Developer
4,  2,         Designer
5,  1,         COO
6,  5,         Officer
....员工结构是:CEO
   --CTO
        --Developer
        --Designer
   --COO
        --Officer
....
现在我想生成一个表table2,用另一种方法来记录员工的信息:ID, Level1Id, Level2Id, Level3Id, Level1Title, Level2Title, Level3Title,
1,  1,        NULL,     NULL,     CEO,         NULL,        NULL
2,  1,        2,        NULL,     CEO,         CTO,         NULL
3,  1,        2,        3,        CEO,         CTO,         Developer
4,  1,        2,        4,        CEO,         CTO,         Designer
5,  1,        5,        NULL,     CEO,         COO,         NULL
6,  1,        5,        6,        CEO,         COO,         Officer

解决方案 »

  1.   


    http://topic.csdn.net/u/20090213/15/b2e1adf6-e45f-41c8-8b1e-987f3c449bc7.html
      

  2.   


    SQLServer2005declare @t table ([ID] int,[ManagerID] int,[Title] varchar(9))
    insert into @t
    select 1,null,'CEO' union all
    select 2,1,'CTO' union all
    select 3,2,'Developer' union all
    select 4,2,'Designer' union all
    select 5,1,'COO' union all
    select 6,5,'Officer'select 
     row_number() over(order by Level1id,level2Id,level3ID)ID
    ,*
    from
    (select
     t1.ID Level1Id
    ,t2.ID Level2Id
    ,t3.ID Level3Id
    ,t1.Title Level1Title
    ,t2.Title Level2Title
    ,t3.Title Level3Title
    from 
    @t t1 left join @t t2
    on t2.managerID = t1.ID
    left join @t t3
    on t3.ManagerID = t2.ID
    union all
    select ID,null,null,Title,null,null
    from @t
    where ManagerID is null
    union all
    select
     t1.ID,t2.ID,null
    ,t1.Title,t2.Title,null
    from 
    @t t1 left join @t t2
    on t2.managerID = t1.ID
    where t1.ManagerID is null
    )tID    Level1Id    Level2Id    Level3Id    Level1Title Level2Title Level3Title
    ----- ----------- ----------- ----------- ----------- ----------- -----------
    1     1           NULL        NULL        CEO         NULL        NULL
    2     1           2           NULL        CEO         CTO         NULL
    3     1           2           3           CEO         CTO         Developer
    4     1           2           4           CEO         CTO         Designer
    5     1           5           NULL        CEO         COO         NULL
    6     1           5           6           CEO         COO         Officer
    7     2           3           NULL        CTO         Developer   NULL
    8     2           4           NULL        CTO         Designer    NULL
    9     3           NULL        NULL        Developer   NULL        NULL
    10    4           NULL        NULL        Designer    NULL        NULL
    11    5           6           NULL        COO         Officer     NULL
    12    6           NULL        NULL        Officer     NULL        NULL
      

  3.   

    没看懂,求父级所有ID?
    /*
    标题:查询各节点的父路径函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
    时间:2008-05-12
    地点:广东深圳
    *//*
    原始数据及要求结果如下:
    --食品 
      --水果 
        --香蕉 
        --苹果    
      --蔬菜 
        --青菜
    id          pid         name                 
    ----------- ----------- -------------------- 
    1           0           食品
    2           1           水果
    3           1           蔬菜
    4           2           香蕉
    5           2           苹果
    6           3           青菜要求得到各节点的父路径即如下结果:
    id  pid name  路径                         
    --- --- ----- ---------------
    1   0   食品  食品
    2   1   水果  食品,水果
    3   1   蔬菜  食品,蔬菜
    4   2   香蕉  食品,水果,香蕉
    5   2   苹果  食品,水果,苹果
    6   3   青菜  食品,蔬菜,青菜 
    */create table tb (id int , pid int , name nvarchar(20)) 
    insert into tb values(1 , 0 , '食品')
    insert into tb values(2 , 1 , '水果')
    insert into tb values(3 , 1 , '蔬菜')
    insert into tb values(4 , 2 , '香蕉')
    insert into tb values(5 , 2 , '苹果')
    insert into tb values(6 , 3 , '青菜')
    go--查询各节点的父路径函数
    create function f_pid(@id int) returns varchar(100)
    as
    begin
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = name from tb where id = @id
      while exists (select 1 from tb where id = @id and pid <> 0)
        begin
          select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
        end
      return @re_str
    end
    goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
    drop function f_pid