select d.仓库号,d.账本号,b.入库数量 as 入库单数,b.入库金额,c.出库数量 as 出库单数,c.出库金额,a.月初金额 from A a
 join B b on b.物资编码=a.物资编码 
 join C c on c.物资编码=b.物资编码 
 join C d on d.物资编码=c.物资编码

解决方案 »

  1.   

    select d.仓库号,d.账本号,b.入库数量 as 入库单数,b.入库金额,c.出库数量 as 出库单数,c.出库金额,a.月初金额 from A a
     join B b on b.物资编码=a.物资编码 
     join C c on c.物资编码=b.物资编码 
     join D d on d.物资编码=c.物资编码
      

  2.   

    select D.仓库号,D.账本号,isnull(B.入库数量,0) 入库单数,isnull(B.入库金额,0) 入库金额,isnull(C.出库数量,0) 出库单数,isnull(C.出库金额,0) 出库金额,isnull(A.出库金额,0) 出库金额,isnull(A.月初金额,0) 月初金额  from D 
    left join (select 物资编码,sum(入库数量) 入库数量,sum(入库金额) 入库金额 from B group by 物资编码) B
      on D.物资编码=B.物资编码
    left join (select 物资编码,sum(出库数量) 出库数量,sum(出库金额) 出库金额 from C group by 物资编码) C
      on D.物资编码=C.物资编码
    left join (select 物资编码,sum(月初数量) 月初数量,sum(月初金额) 月初金额 from A group by 物资编码) A
      on D.物资编码=A.物资编码
      

  3.   

    nboys() 是不正确的。我用过得不到结果
      

  4.   

    select D.仓库号,D.账本号,isnull(B.入库数量,0) 入库单数,isnull(B.入库金额,0) 入库金额,isnull(C.出库数量,0) 出库单数,isnull(C.出库金额,0) 出库金额,isnull(A.出库金额,0) 出库金额,isnull(A.月初金额,0) 月初金额  from D 
    full join (select 物资编码,sum(入库数量) 入库数量,sum(入库金额) 入库金额 from B group by 物资编码) B
      on D.物资编码=B.物资编码
    Full join (select 物资编码,sum(出库数量) 出库数量,sum(出库金额) 出库金额 from C group by 物资编码) C
      on D.物资编码=C.物资编码
    Full join (select 物资编码,sum(月初数量) 月初数量,sum(月初金额) 月初金额 from A group by 物资编码) A
      on D.物资编码=A.物资编码
      

  5.   

    pengdali与CrazyFor 你们能不能说说left join 与full 与left join 这两个得到的结果是不一样的都有重复的数据出现
    应该得到的是这样,
      仓库号  账本号    入库单数    入库金额  出库单数   出库金额   月初金额
       
        01      01        2            3         1          1          3
        01      02        0            0         1          2          4
        02      01        1            3         2          7          3
        02      02        2            9         0          0          0
    面你们得到的是
      仓库号  账本号    入库单数    入库金额  出库单数   出库金额   月初金额
          01    01        1                1     0          0           1
          01    01        。。
          01    02        
          01    02
          01    02
          02    02出现了仓库号与账本号重复了(即仓库与账本组全起来不应该出现重复)
      

  6.   

    select D.仓库号,D.账本号,isnull(sum(B.入库数量),0) 入库单数,isnull(sum(B.入库金额),0) 入库金额,isnull(sum(C.出库数量),0) 出库单数,isnull(sum(C.出库金额),0) 出库金额,isnull(sum(A.出库金额),0) 出库金额,isnull(sum(A.月初金额),0) 月初金额  
    from D
    left join (select 物资编码,sum(入库数量) 入库数量,sum(入库金额) 入库金额 from B group by 物资编码) B
      on D.物资编码=B.物资编码
    left join (select 物资编码,sum(出库数量) 出库数量,sum(出库金额) 出库金额 from C group by 物资编码) C
      on D.物资编码=C.物资编码
    left join (select 物资编码,sum(月初数量) 月初数量,sum(月初金额) 月初金额 from A group by 物资编码) A
      on D.物资编码=A.物资编码 group by D.仓库号,D.账本号