现有A表数据,字段为: 
no,upno,lev 
11     22         1 
22     33         2   
33     44         3 现在我想取数据11   33   1的数据如何写sql语句啊? 
11   44   1   呢? 
谢谢啦

解决方案 »

  1.   

    select no,upno,lev
    from 
    (
    select no,0 as upno,lev from A where no=11
    union
    select 0 as no,upno,0 as lev from A where upno=33
    ) as tb出来的结果就是11  33  1把33换成44就是11  44  1
      

  2.   


    楼主自己想出来的算法吧?
    既然这样,那结果就是:
    select 11,33,1
    呵呵```
      

  3.   

    不好意思,下午临时有事,需求是这样的
    我有表A,字段no部门,upno上级部门,level级别
    表内容:11  22 1(11部门的上级部门是22,11部门的级别是1级)
            aa  22 1
            22  33 2
           bb  33 2
           33  44 3
    现在我想将表A的数据重新整合一下,将3级部门不变,2级也不动,1级的关连到上级部门的上级,表B 与A机构一样。
    也就是结果如下:
    11  33  1
    aa  33  1
    22  33  2
    bb  33  2
    33  44  3
    不知道大家明白没有?
      

  4.   

    CREATE TABLE Test (
    [No] nchar(10) , 
    [UpNo] nchar(10),
    [Level] int )
    GOInsert Test ([No],[UpNo],[Level]) 
    Select  '11', '22', 1
    Union Select 'aa', '22', 1
    Union  Select '22', '33', 2 
    Union  Select 'bb', '33', 2              
    Union  Select '33', '44', 3 Update Test Set Test.UpNo =  (Select Test.UpNo 
    From (Select Distinct UpNo From Test Where Level=1) a,
    Test Where a.UpNo = Test.No)
    Where Exists( Select a.UpNo 
    From (Select Distinct UpNo From Test Where Level=1) a,
    Test Where a.UpNo = Test.No)
    And Test.Level = 1 --drop table Test
      

  5.   


    --按结果写
    declare  @T  TABLE      ( [No]   nchar(10)   , [UpNo]   nchar(10), [Level]   int   ) Insert   @T   ([No],[UpNo],[Level])   
    Select     '11',   '22',   1 
    Union     Select   '22',   '33',   2                              
    Union     Select   '33',   '44',   3   
    select 
    distinct t.No,t2.UPNO,t.Level
    from 
    @T t join @T t2 on t.NO<>t2.No
    where
    T.No='11'
    (所影响的行数为 3 行)No         UPNO       Level       
    ---------- ---------- ----------- 
    11         33         1
    11         44         1(所影响的行数为 2 行)
      

  6.   

    declare  @T  TABLE      ( [No]   nchar(10)   , [UpNo]   nchar(10), [Level]   int   ) Insert   @T   ([No],[UpNo],[Level])   
    Select     '11',   '22',   1 
    Union     Select   '22',   '33',   2                              
    Union     Select   '33',   '44',   3   
    select 
     t.No,t2.UPNO,t.Level
    from 
    @T t join @T t2 on t.[Level]=t2.[Level]-1
    where
    T.No='11'
    select 
     t.No,
    t2.UPNO,
    t.Level
    from 
    @T t ,@T t2
    where
    T.No='11' and not exists(select 1 from @T where [Level]>t2.[Level])
    /*(所影响的行数为 3 行)No         UPNO       Level       
    ---------- ---------- ----------- 
    11         33         1(所影响的行数为 1 行)No         UPNO       Level       
    ---------- ---------- ----------- 
    11         44         1(所影响的行数为 1 行)
    */