表1.
CODE PARENT_CODE 
1
1.1 1
1.2 1
2
2.1 2
2.1.1 2.1
2.1.1.1 2.1.1
2.2 2
3
4
表2
CODE YEAR_QTY
2.1.1.1 54.51
1.1 432.97
3 52.81
希望查到的结果是
CODE PARENT_CODE YEAR_QTY
1
1.1 1.1 432.97
2
2.1 2
2.1.1 2.1
2.1.1.1 2.1.1 54.51
3 52.81就是以表2,查到表2和表1中相同的CODE以及CODE的父级,求解答

解决方案 »

  1.   

    select * from 表1 A left join  表2 B on A.code=B.code
    ???
      

  2.   

    不是的,请看我给的结果,
    要根据表2的CODE来查询。
    表2的CODE作为最子结果来查询它的父节点,以及父节点的上级
      

  3.   


    --> 测试数据: [表1]
    if object_id('[表1]') is not null drop table [表1]
    create table [表1] (CODE varchar(7),PARENT_CODE varchar(5))
    insert into [表1]
    select '1',null union all
    select '1.1','1' union all
    select '1.2','1' union all
    select '2',null union all
    select '2.1','2' union all
    select '2.1.1','2.1' union all
    select '2.1.1.1','2.1.1' union all
    select '2.2','2' union all
    select '3',null union all
    select '4',null
    --> 测试数据: [表2]
    if object_id('[表2]') is not null drop table [表2]
    create table [表2] (CODE varchar(7),YEAR_QTY numeric(5,2))
    insert into [表2]
    select '2.1.1.1',54.51 union all
    select '1.1',432.97 union all
    select '3',52.81
    gowith wsp
    as
    (
    select a.code,b.parent_code,a.year_qty from [表2] a,表1 b where a.code=b.code
    union all
    select a.*,null from 表1 a,wsp b where a.code=b.parent_code
    )
    select * from wsp order by code--结果:
    code    parent_code year_qty
    ------- ----------- -------------
    1       NULL        NULL
    1.1     1           432.97
    2       NULL        NULL
    2.1     2           NULL
    2.1.1   2.1         NULL
    2.1.1.1 2.1.1       54.51
    3       NULL        52.81
      

  4.   

    你这是用SQL 2005的么。。
    有用SQL 2000的么
      

  5.   

    ---用cte递归
    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-09-08 23:39:25
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[表1]
    if object_id('[表1]') is not null drop table [表1]
    go 
    create table [表1]([CODE] varchar(7),[PARENT_CODE] varchar(5))
    insert [表1]
    select '1',null union all
    select '1.1','1' union all
    select '1.2','1' union all
    select '2',null union all
    select '2.1','2' union all
    select '2.1.1','2.1' union all
    select '2.1.1.1','2.1.1' union all
    select '2.2','2' union all
    select '3',null union all
    select '4',null
    --> 测试数据:[表2]
    if object_id('[表2]') is not null drop table [表2]
    go 
    create table [表2]([CODE] varchar(7),[YEAR_QTY] numeric(5,2))
    insert [表2]
    select '2.1.1.1',54.51 union all
    select '1.1',432.97 union all
    select '3',52.81
    --------------开始查询--------------------------
    ;with f as
    (
        select a.code,b.parent_code,a.year_qty from [表2] a,表1 b where a.code=b.code
        union all
        select a.*,null from 表1 a,f b where a.code=b.parent_code
    )
    select * from f order by code----------------结果----------------------------
    /* code    parent_code year_qty
    ------- ----------- ---------------------------------------
    1       NULL        NULL
    1.1     1           432.97
    2       NULL        NULL
    2.1     2           NULL
    2.1.1   2.1         NULL
    2.1.1.1 2.1.1       54.51
    3       NULL        52.81(7 行受影响)
    */
      

  6.   

    ---2000参考 不写了
    ----------------------------------------------------------------------------------------------------------------
    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
      

  7.   

    用WITH的是SQL2005的就不要给了,
    谢谢大家,
    fredrickhu,我套用你的试试看
      

  8.   


    2000:
    --> 测试数据: [表1]
    if object_id('[表1]') is not null drop table [表1]
    create table [表1] (CODE varchar(7),PARENT_CODE varchar(5))
    insert into [表1]
    select '1',null union all
    select '1.1','1' union all
    select '1.2','1' union all
    select '2',null union all
    select '2.1','2' union all
    select '2.1.1','2.1' union all
    select '2.1.1.1','2.1.1' union all
    select '2.2','2' union all
    select '3',null union all
    select '4',null
    --> 测试数据: [表2]
    if object_id('[表2]') is not null drop table [表2]
    create table [表2] (CODE varchar(7),YEAR_QTY numeric(5,2))
    insert into [表2]
    select '2.1.1.1',54.51 union all
    select '1.1',432.97 union all
    select '3',52.81
    go
    create function wsp()
    returns @t table(code varchar(10),parent_code varchar(10),year_qty numeric(5,2),level int)
    as
    begin
    declare @level int
    set @level=1
    insert into @t select a.code,b.parent_code,a.year_qty,@level from [表2] a,表1 b where a.code=b.code
    while(@@rowcount>0)
    begin
    set @level=@level+1
    insert into @t select a.*,null,@level from 表1 a,@t b where a.code=b.parent_code and b.level=@level-1
    end
    return 
    end
    goselect * from dbo.wsp() order by code
      

  9.   

    pt1314917,谢谢!!非常感谢
    结贴了~~~
    能交个朋友么,我QQ172716811
    邮箱[email protected]