Select C.仓库编号,A.仓库名称,D.货品编号,B.货品名称,SUM(D.数量) AS 进货总数量,SUM(金额) AS 进货总金额
From 
StoreHouse A INNER JOIN InBase C ON A.仓库编号=C.仓库编号
INNER JOIN InList D ON C. 单据号=D. 单据号
INNER JOIN Goods B ON D. 货品编号=B. 货品编号
GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称

解决方案 »

  1.   

    不对啊!!!
    我要列出所有的记录,假如StoreHouse中有10个仓库,Goods中有1000个货品,那么对应要有10*1000条记录,一个仓库对应1000个货品
      

  2.   

    Select C.仓库编号,A.仓库名称,D.货品编号,B.货品名称,SUM(D.数量) AS 进货总数量,SUM(金额) AS 进货总金额
    From 
    StoreHouse A INNER JOIN InBase C ON A.仓库编号=C.仓库编号
    INNER JOIN InList D ON C. 单据号=D. 单据号
    left   JOIN Goods B ON D. 货品编号=B. 货品编号
    GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称
      

  3.   

    SELECT C.仓库编号, A.仓库名称, D .货品编号, B.货品名称, SUM(D .数量) 
                  AS 进货总数量, SUM(金额) AS 进货总金额
            FROM StoreHouse A INNER JOIN
                  InBase C ON A.仓库编号 = C.仓库编号 INNER JOIN
                  InList D ON C.单据号 = D .单据号 FULL OUTER JOIN
                  Goods B ON D .货品编号 = B.货品编号
            GROUP BY C.仓库编号, A.仓库名称, D .货品编号, B.货品名称
      

  4.   

    这样吧:
    select *,
           (select isnull(sum(数量),0)
            from (select 仓库编号,
                         货品编号,
                         数量
                  from InBase INNER JOIN
                       InList
                              ON InBase.单据号=InList.单据号
                 ) as B
            where B.仓库编号=A.仓库编号
              and B.货品编号=A.货品编号
           ) as 进货总数量,
           (select isnull(sum(金额),0)
            from (select 仓库编号,
                         货品编号,
                         金额
                  from InBase INNER JOIN
                       InList
                              ON InBase.单据号=InList.单据号
                 ) as B
            where B.仓库编号=A.仓库编号
              and B.货品编号=A.货品编号
           ) as 进货总金额
    from (select StoreHouse.仓库编号,
                 StoreHouse.仓库名称,
                 Goods.货品编号,
                 Goods.货品名称
          from Goods,StoreHouse
         ) as A
    order by 仓库编号,货品编号
      

  5.   

    我这样建表不知道是否满足要求:
    对InList表,考虑同一单不应该对同一种货物记两笔;
    同样,同一个仓库也不应该对同一种货物记两笔,所以PK(FInBasId,FGoodsId)
    create table StoreHouse
    (
    FStoreId int not null PRIMARY KEY IDENTITY(0,1),
    FName varchar(255) not null UNIQUE 

    )create table Goods
    (
    FGoodsId int not null PRIMARY KEY IDENTITY(0,1),
    FName varchar(255) not null UNIQUE
    )create table InBas
    (
    FInBasId int not null PRIMARY KEY IDENTITY(0,1),
    FStoreId int not null,
    CONSTRAINT FStoreId_FK FOREIGN KEY (FStoreId) REFERENCES StoreHouse(FStoreId)
    )create table InList
    (
    FInBasId int not null,
    FGoodsId int not null,
    FQuantity int not null DEFAULT 0,
    FSum money not null DEFAULT 0,
    CONSTRAINT FInBasId_FGoodsId_PK PRIMARY KEY (FInBasId,FGoodsId),
    CONSTRAINT FInBasId_FK FOREIGN KEY (FInBasId) REFERENCES InBas(FInBasId),
    CONSTRAINT FGoodsId_FK FOREIGN KEY (FGoodsId) REFERENCES Goods(FGoodsId)
    )--本来一个SQL就可以搞定,但我在SQL7.0下执行下句时会报意外错,只好先写入临时表(两句完成)
    select AllGoods.fstoreid,AllGoods.fgoodsid,FQuantity=isnull(i.FQuantity,0),FSum=isnull(i.Fsum,0) from 
    (select storel.fstoreid,g.fgoodsid from
    (select distinct i2.fstoreid 
     from inlist i1,inbas i2 where i1.finbasid=i2.finbasid) storeL,goods g
    ) AllGoods
    left outer join (select i2.FStoreid,i1.Fgoodsid,i1.FQuantity,i1.FSum from inlist i1,inbas i2 where i1.Finbasid=i2.finbasid) i
    on AllGoods.FGoodsid=i.FGoodsid and Allgoods.FStoreid=i.FStoreid--解:
    --1.
    select storel.fstoreid,g.fgoodsid into #temp from
    (select distinct i2.fstoreid 
     from inlist i1,inbas i2 where i1.finbasid=i2.finbasid) storeL,goods g
    --2.
    select s.FStoreid,s.FName,g.fgoodsid,g.FName,e.FQuantity,e.FSum from
    (select #temp.fstoreid,#temp.fgoodsid,FQuantity=sum(isnull(i.FQuantity,0)),FSum=sum(isnull(i.Fsum,0)) from 
    #temp
    left outer join (select i2.FStoreid,i1.Fgoodsid,i1.FQuantity,i1.FSum from inlist i1,inbas i2 where i1.Finbasid=i2.finbasid) i
    on #temp.FGoodsid=i.FGoodsid and #temp.FStoreid=i.FStoreid 
    group by #temp.fstoreid,#temp.fgoodsid
    ) e
    left outer join Storehouse s on e.fstoreid=s.fstoreid
    left outer join Goods g on g.fgoodsid=e.fgoodsid
    order by e.FStoreid,e.FGoodsid以上在SQL7.0下通过;执行过程中会作一次笛卡儿乘积,看来表的设计有问题!
      

  6.   

    To  iihorse(老象腿):
      同一个仓库不应该对同一种货物记两笔,这一点你判断正确,但是在InList表中,同一单要对同一种货物记多笔,这是那个变态公司要求的,呵呵,说这是做帐上的要求,因为这样可以看见进货的细节流程。
      你所写的我通过不了,我再看看!!
      

  7.   

    To hyhy95(无相)
       如果是这样的话,那么同一单涉及的各种货物是否允许取至不同的仓库呢?也就说:InBas表是否有PK;请最好给出各个表的键值情况。
      

  8.   

    Try:
    Select C.仓库编号,A.仓库名称,D.货品编号,B.货品名称,SUM(D.数量) AS 进货总数量,SUM(金额) AS 进货总金额
    From 
    StoreHouse A,InBase C,InList D,Goods B
    where A.仓库编号=C.仓库编号
    and C. 单据号=D. 单据号
    and  D. 货品编号=*B. 货品编号
    GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称