进货表table1
barcode(商品编号)  goodsname(商品名称)   qty(进货数量)  price(进货单价)  jinhuoriqi(进货日期)
  0001                  热水器                 5            789             2011-06-09
销售表table2
barcode(商品编号)  goodsname(商品名称)   qty(销售数量)  price(销售单价)  jinhuoriqi(销售日期)
  0001                  热水器                 5            789             2011-06-09汇总表table3(汇总表统计当天某一商品的进货数量和销售数量,如果进货日期和销售日期一样就显示一条记录,也就是当天即有进货又有销售。如果销售日期和进货日期不一样则新增一条记录。其实就是一张进销存汇总表)
barcode(商品编号)  goodsname(商品名称) qty(进货数量)  price(进货单价)   qty(销售数量)  price(销售单价)  jinhuoriqi(日期)

解决方案 »

  1.   

    select a.商品编号,a.商品名称,a.进货数量,a.进货单价,b.销量数量,b.销售单价,b.销售日期 from table1 a left join table 2 b on a.商品编号=b.商品编号
      

  2.   


    select isnull(a.barcode,b.barcode),isnull(a.goodsname,b.goodsname),
           isnull(进货数量,0),isnull(销售数量,0),isnull(销售单价,0),
           isnull(a.jinhuoriqi,b.jinhuoriqi)
    from table1 a 
    full join table2 b 
    on a.barcode=b.barcode and a.jinhuoriqi=b.jinhuoriqi
      

  3.   

    select
     isnull(a.barcode,b.barcode) as barcode,
     isnull(a.goodsname,b.goodsname) as goodsname,
     isnull(a.price,0) as [price(进货单价)],
     isnull(a.qty,0) as [qty(进货数量)],
     isnull(b.qty,0) as [qty(销售数量)],
     isnull(b.price,0) as [ price(销售单价)],
     isnull(a.jinhuoriqi,b.jinhuoriqi) as [jinhuoriqi(日期)]
    from
     table1 a 
    full join
     table2 b 
    on
     a.barcode=b.barcode 
    and
     a.jinhuoriqi=b.jinhuoriqi