我在SQL SERVER2000后台建立了3张表,表a1,a2,a3结构如下:
a1:                a
代码  数量(12月) 
111   1
112   3
115   10
a2
代码  数量(11月)
111   2
115  12
112   11
114   5
a2
代码  数量(10月) 
111   6
112   8
115   7
110   20
211   15
问题如下:我想把以上三张表合并成一张表A,主要思想是以a1表为基准,最终我想要的结果如下:
代码   数量(12月)   数量(11月)   数量(10月)
111     1            2           6
112     3            11          8
115     10           12          7
请问怎么写SQL代码??如果以后我的表增加到24张表,a1,a2,a3,a4------a24,我还是以a1表为基准,如何把它们合并成一张表A????

解决方案 »

  1.   

    select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月]
    from a1 a , a2 b , a3 c
    where a.代码 = b.代码 and a.代码 = c.代码
      

  2.   

    select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月] , d.数量 [09月] ... 
    from a1 a , a2 b , a3 c  ,a4 d ...
    where a.代码 = b.代码 and a.代码 = c.代码 and a.代码 = d.代码 ...
      

  3.   

    select a.代码 , a.数量 [12月] , b.数量 [11月] , c.数量 [10月]
    from a1 a left join a2 b on a.代码 = b.代码
      left join a3 c on a.代码 = c.代码
    用left join这样可以保证即使11月、10月中某个代码不存在,也能查出数据。另外楼主想自动根据日期增长,按要求先动态组合好语句,再查询就行了,而不是由
    SQL语句来动态处理。
      

  4.   

    select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月] , d.数量 [09月] ... 
    from a1 a , a2 b , a3 c  ,a4 d ...
    where a.代码 = b.代码 and a.代码 = c.代码 and a.代码 = d.代码 ...
      

  5.   


    select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月] , d.数量 [09月] ... 
    from a1 a , a2 b , a3 c  ,a4 d ...
    where a.代码 = b.代码 and a.代码 = c.代码 and a.代码 = d.代码 ...
      

  6.   

    关键是你09年的数据中的表有什么规律吗?select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月] , d.数量 [09月] ... 
    from a1 a , a2 b , a3 c  ,a4 d ...
    where a.代码 = b.代码 and a.代码 = c.代码 and a.代码 = d.代码 ...
      

  7.   

    用连接有表个数限制
    连接如果用楼上各位写法是不对的,应该是用a1 lef join 其他表,否则万一哪个表少个代码,就丢失数据了
      

  8.   

    建议建新查询表,用存储过程更新数据,得到报表。newtable:
    代码,1月数量,2月数量,3月数量然后:
    insert newtable(代码) select * from 代码表update newtable
    set 1月数量=a1.数量
    from newtable,a1
    where newtable.代码=a1.代码
    update newtable
    set 2月数量=a2.数量
    from newtable,a2
    where newtable.代码=a2.代码。delete from newtable
    where 1月数量=null and 2月数量=null and .....select * from newtable
      

  9.   


    select a.代码 , a.数量 [10月] , b.数量 [11月] , c.数量 [10月] , d.数量 [09月] ... 
    from a1 a , a2 b , a3 c  ,a4 d ...
    where a.代码 = b.代码 and a.代码 = c.代码 and a.代码 = d.代码 ...
      

  10.   


    declare @a1 table([代码] int,[数量] int)
    insert @a1
    select 111,1 union all
    select 112,3 union all
    select 115,10declare @a2 table([代码] int,[数量] int)
    insert @a2
    select 111,2 union all
    select 115,12 union all
    select 112,11 union all
    select 114,5declare @a3 table([代码] int,[数量] int)
    insert @a3
    select 111,6 union all
    select 112,8 union all
    select 114,9 union all
    select 115,7 union all
    select 110,20 union all
    select 211,15select t.代码, a1.数量 [12月] , a2.数量 [11月] , a3.数量 [10月] 
    from
    (
    select 代码 from @a1 union
    select 代码 from @a2 union
    select 代码 from @a3 
    )t
    left join @a1 a1 on a1.代码=t.代码
    left join @a2 a2 on a2.代码=t.代码
    left join @a3 a3 on a3.代码=t.代码 
    /*
    代码 12月  11月  10月
    110  NULL NULL 20
    111  1    2    6
    112  3    11   8
    114  NULL 5    9
    115  10   12   7
    211  NULL NULL 15
    */
      

  11.   

    Declare @A1 Table(Code Varchar(3),Quantity int)
    Declare @A2 Table(Code Varchar(3),Quantity int)
    Declare @A3 Table(Code Varchar(3),Quantity int)Insert @A1 
    Select '111',1
    Union
    Select '112',3
    Union
    Select '115',10Insert @A2 
    Select '111',2
    Union
    Select '115',12
    Union
    Select '112',11
    Union
    Select '114',5Insert @A3 
    Select '111',6
    Union
    Select '112',8
    Union
    Select '115',7
    Union
    Select '110',20
    Union
    Select '211',15Select A1.Code '代码',A1.Quantity '数量(12月)',A2.Quantity '数量(11月)',A3.Quantity '数量(10月)' From @A1 A1
    Left Join 
    @A2 A2
    On A1.Code = A2.Code
    Left Join 
    @A3 A3
    On A1.Code = A3.Code
    如果有其他月,则依次Left Join就好了
      

  12.   

    select a1.代码,a1.数量 as [12月],a2.数量 as [11月],a3.数量 as [10月]
    from 
        a1
    left join
        a2
    on
        a1.代码 = a2.代码
    left join
        a3
    on
        a1.代码 = a3.代码
    where
        a2.数量 is not null
    and
        a3.数量 is not null