是这样的一个表,内容大致如下:
ID 姓名 直接上级
1 a --
2 b a
3 c b
4 d c
5 e c
6 f c
7 g b
8 h g
9 i g
10 j g
11 k b
12 l k
13 m k
14 n k
所要查询的结果是这样:总经理             a
副总            b
主管        c    g k
员工       def   hij lmn其最终目的就是利用直接上级字段,把公司员工分级显示出来。
求解决语句。
帮忙了!谢谢

解决方案 »

  1.   


    create table tb(ID int, 姓名 varchar(100), 直接上级 varchar(100))
    insert tb select 
    1, 'a', null union all select 
    2 , 'b','a'  union all select 
    3 , 'c','b'  union all select  
    4 , 'd','c'  union all select  
    5 , 'e','c'  union all select  
    6 , 'f','c'  union all select  
    7 , 'g','b'  union all select  
    8 , 'h','g'  union all select  
    9 , 'i','g'  union all select  
    10 , 'j','g'  union all select  
    11 , 'k','b'  union all select  
    12 , 'l','k'  union all select  
    13 , 'm','k'  union all select  
    14 , 'n','k'   
    go
    select * from tbwith cte as
    (
    select ID,姓名,直接上级,lvl=1 from tb where 直接上级 is null
    union all
    select a.ID,a.姓名,a.直接上级,lvl=b.lvl+1 from tb a join cte b on a.直接上级=b.姓名
    )
    select 职位=case lvl when 1 then '总经理' when 2 then '副总' when 3 then '主管' else '员工' end, 姓名=stuff((select ' '+姓名 from cte where lvl=c.lvl for xml path('')),1,1,'') 
    from cte c
    group by lvl
    /*
    职位     姓名
    ------ ----------------------
    总经理    a
    副总     b
    主管     c g k
    员工     l m n h i j d e f
    */
    drop table tb
      

  2.   

    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid
      

  3.   

    /*
    标题:查询指定节点及其所有父节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有父节点的函数
    create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
    as
    begin
      insert into @t_level select @id
      select @id = pid from tb where id = @id and pid is not null
      while @@ROWCOUNT > 0
      begin
        insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
      end
      return
    end
    go--调用函数查询002(广州市)及其所有父节点
    select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有父节点
    select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市(所影响的行数为 2 行)
    */--调用函数查询008(西乡镇)及其所有父节点
    select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市
    007  003  宝安区
    008  007  西乡镇(所影响的行数为 4 行)
    */drop table tb
    drop function f_pid
      

  4.   

    修正版:create table tb(ID int, 姓名 varchar(100), 直接上级 varchar(100))
    insert tb select 
    1, 'a', null union all select 
    2 , 'b','a'  union all select 
    3 , 'c','b'  union all select  
    4 , 'd','c'  union all select  
    5 , 'e','c'  union all select  
    6 , 'f','c'  union all select  
    7 , 'g','b'  union all select  
    8 , 'h','g'  union all select  
    9 , 'i','g'  union all select  
    10 , 'j','g'  union all select  
    11 , 'k','b'  union all select  
    12 , 'l','k'  union all select  
    13 , 'm','k'  union all select  
    14 , 'n','k'   
    go
    select * from tbwith cte as
    (
    select ID,姓名,直接上级,lvl=1 from tb where 直接上级 is null
    union all
    select a.ID,a.姓名,a.直接上级,lvl=b.lvl+1 from tb a join cte b on a.直接上级=b.姓名
    )
    select 职位=case lvl when 1 then '总经理' when 2 then '副总' when 3 then '主管' else '员工' end
    , 姓名=case lvl when 4 then stuff((select ' '+姓名 from 
    (
    select 直接上级,姓名=(select ''+姓名 from cte where 直接上级=c.直接上级 for xml path(''))
    from cte c
    where lvl=4
    group by 直接上级
    ) t for xml path('')),1,1,'')
    else stuff((select ' '+姓名 from cte where lvl=c.lvl for xml path('')),1,1,'') end
    from cte c
    group by lvl/*
    职位     姓名
    ------ ----------------------
    总经理    a
    副总     b
    主管     c g k
    员工     def hij lmn
    */
    drop table tb