问题如下: 
一季度表 
CITY  BOOK_ID  BOOK_ACCOUNT_JAN  BOOK_ACCOUNT_FEB  BOOK_ACCOUNT_MAR
上海      001        100                NULL                NULL
上海      002         50                NULL                NULL
上海      003         60                NULL                NULL
北京      001         80                NULL                NULL
广州      001         50                NULL                NULL二月表
CITY  BOOK_ID  BOOK_ACCOUNT_FEB
上海      001        100                        
上海      002        100       
上海      003        100       
北京      001        100       
广州      001        100       
广州      002        100       
----------------------------------- 
如下SQL语句如何书写?
问题一:将二月表与一季度表中[ID和城市]相同的BOOK_ACCOUNT_FEB填入到一季度表中
答:
update 一季度表  set BOOK_ACCOUNT_FEB = 二月表.BOOK_ACCOUNT_FEB
from 二月表
where 一季度表.BOOK_ID=二月表.BOOK_ID and 一季度表.CITY=二月表.CITY问题二:将一季度表中没有的[ID和城市]添加到一季度表中,如二月表中的: 广州      002        100 
答:这个怎么写??结果如下: 
一季度表 
CITY  BOOK_ID  BOOK_ACCOUNT_JAN  BOOK_ACCOUNT_FEB  BOOK_ACCOUNT_MAR
上海      001        100                100                NULL
上海      002         50                100                NULL
上海      003         60                100                NULL
北京      001         80                100                NULL
广州      001         50                100                NULL
广州      002         NULL              100                NULL

解决方案 »

  1.   


    declare @yj table (city nvarchar(10),book_id nvarchar(10),book_account_jan int,
                       book_account_feb int,book_account_mar int)
    insert into @yj select '上海','001',100,null,null
          union all select '上海','002',50,null,null
          union all select '上海','003',60,null,null
          union all select '北京','001',80,null,null
          union all select '广州','001',50,null,null
    declare @ay table (city nvarchar(10),book_id nvarchar(10),book_account_feb int)
    insert into @ay select '上海',001,100
          union all select '上海',002,100
          union all select '上海',003,100
          union all select '北京',001,100
          union all select '广州',001,100
          union all select '广州',002,100
    update a set a.book_account_feb=b.book_account_feb    from @yj a join @ay b on a.city=b.city and a.book_id=b.book_id
    insert into @yj(city,book_id,book_account_feb) select * from @ay a where not exists (select 1 from @yj b where b.book_id=a.book_id and a.city=b.city)
    select * from @yjcity       book_id    book_account_jan book_account_feb book_account_mar
    ---------- ---------- ---------------- ---------------- ----------------
    上海         001        100              NULL             NULL
    上海         002        50               NULL             NULL
    上海         003        60               NULL             NULL
    北京         001        80               NULL             NULL
    广州         001        50               NULL             NULL
    上海         1          NULL             100              NULL
    上海         2          NULL             100              NULL
    上海         3          NULL             100              NULL
    北京         1          NULL             100              NULL
    广州         1          NULL             100              NULL
    广州         2          NULL             100              NULL(11 行受影响)
      

  2.   


    declare @yj table (city nvarchar(10),book_id nvarchar(10),book_account_jan int,
                       book_account_feb int,book_account_mar int)
    insert into @yj select '上海','001',100,null,null
          union all select '上海','002',50,null,null
          union all select '上海','003',60,null,null
          union all select '北京','001',80,null,null
          union all select '广州','001',50,null,null
    declare @ay table (city nvarchar(10),book_id nvarchar(10),book_account_feb int)
    insert into @ay select '上海','001',100
          union all select '上海','002',100
          union all select '上海','003',100
          union all select '北京','001',100
          union all select '广州','001',100
          union all select '广州','002',100
    update a set a.book_account_feb=b.book_account_feb    from @yj a join @ay b on a.city=b.city and a.book_id=b.book_id
    insert into @yj(city,book_id,book_account_feb) select * from @ay a where not exists (select 1 from @yj b where b.book_id=a.book_id and a.city=b.city)
    select * from @yjcity       book_id    book_account_jan book_account_feb book_account_mar
    ---------- ---------- ---------------- ---------------- ----------------
    上海         001        100              100              NULL
    上海         002        50               100              NULL
    上海         003        60               100              NULL
    北京         001        80               100              NULL
    广州         001        50               100              NULL
    广州         002        NULL             100              NULL(6 行受影响)