select语句不知怎写,请大家帮忙
table :仓库凭证
field :仓库 int,凭证号 char ,物品 int,进出 bit, 数量 num,金额 numTable :库存
Field :仓库 int,物品 int,数量 num,金额 num求得出结果:
Sum(仓库凭证.数量) as 进仓数量 Where 进出 = 1
Sum(仓库凭证.金额) as 进仓金额 Where 进出 = 1
Sum(仓库凭证.数量) as 出仓数量 Where 进出 = 0
Sum(仓库凭证.金额) as 出仓金额 Where 进出 = 0
库存.数量 as 期末数量
库存.金额 as 期末金额期末数量 - 进仓数量 + 出仓数量 as 期初数量
期末金额 - 进仓金额 + 出仓金额 as 期初金额

解决方案 »

  1.   


     
    select 
        A.仓库,
        A.物品,
        isnull(B.数量,0)-isnull(A.进仓数量,0)+isnull(A.出仓数量,0) AS 期初数量,
        isnull(B.金额,0)-isnull(A.进仓金额,0)+isnull(A.出仓金额,0) AS 期初金额,
        A.进仓数量,
        A.进仓金额,
        A.出仓数量,
        A.出仓金额,
        B.数量 as 期末数量,
        B.金额 as 期末金额
    from (
    select 
         仓库,
         物品,
         sum(case when 进出=1 then 数量 else 0 end ) as 进仓数量,
         sum(case when 进出=1 then 金额 else 0 end ) as 进仓金额,
         sum(case when 进出=0 then 数量 else 0 end ) as 出仓数量,
         sum(case when 进出=0 then 金额 else 0 end ) as 出仓金额
    from 仓库凭证
    group by 仓库,物品) AS A
        inner join 库存 AS B on A.仓库=B.仓库 and A.物品=B.物品
      

  2.   


    create table #仓库凭证(仓库 int,凭证号 char ,物品 int,进出 bit, 数量 int,金额 int)create Table #库存(仓库 int,物品 int,数量 int,金额 int) 
    select 
        A.仓库,
        A.物品,
        isnull(B.数量,0)-isnull(A.进仓数量,0)+isnull(A.出仓数量,0) AS 期初数量,
        isnull(B.金额,0)-isnull(A.进仓金额,0)+isnull(A.出仓金额,0) AS 期初金额,
        A.进仓数量,
        A.进仓金额,
        A.出仓数量,
        A.出仓金额,
        B.数量 as 期末数量,
        B.金额 as 期末金额
    from (
    select 
         仓库,
         物品,
         sum(case when 进出=1 then 数量 else 0 end ) as 进仓数量,
         sum(case when 进出=1 then 金额 else 0 end ) as 进仓金额,
         sum(case when 进出=0 then 数量 else 0 end ) as 出仓数量,
         sum(case when 进出=0 then 金额 else 0 end ) as 出仓金额
    from #仓库凭证
    group by 仓库,物品) AS A
        inner join #库存 AS B on A.仓库=B.仓库 and A.物品=B.物品  
    drop table #仓库凭证,#库存
      

  3.   

    来一个例子:
    declare @仓库凭证 table(仓库 int,凭证号 char(3) ,物品 varchar(5),进出 bit, 数量 int,金额 decimal(9,3))
    insert into @仓库凭证
    select '1','001','B',1,100,1000 union all
    select '1','002','B',0,40,400 union all
    select '1','003','B',1,50,500declare @库存 table (仓库 int,物品 varchar(5),数量 int,金额 decimal(9,3))
    insert into @库存
    select '1','B',110,1100select tmp.仓库,tmp.物品,tmp.凭证号,
    [库初数量]
    =case when b.数量<tmp.StocksSum+tmp.数量 then b.数量-tmp.StocksSum else tmp.数量 end
    from 
    (select *,StocksSum=
    isnull((select sum(case when [进出]=1 then [数量] else -[数量] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]>a.[凭证号]),0),
    StocksPrice=
    isnull((select sum(case when [进出]=1 then [金额] else -[金额] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]>a.[凭证号]),0)
    from @仓库凭证 as a )tmp 
    right join @库存 b  on tmp.仓库=b.仓库 and tmp.物品=b.物品
    and b.数量 !<tmp.StocksSum+tmp.数量 
    (3 行受影响)(1 行受影响)
    仓库          物品    凭证号  库初数量
    ----------- ----- ---- -----------
    1           B     001  100
    1           B     002  40
    1           B     003  50(3 行受影响)
      

  4.   

    declare @仓库凭证 table(仓库 int,凭证号 char(3) ,物品 varchar(5),进出 bit, 数量 int,金额 decimal(9,3))
    insert into @仓库凭证
    select '1','001','B',1,100,1000 union all
    select '1','002','B',0,40,400 union all
    select '1','003','B',1,50,500declare @库存 table (仓库 int,物品 varchar(5),数量 int,金额 decimal(9,3))
    insert into @库存
    select '1','B',110,1100select tmp.仓库,tmp.物品,tmp.凭证号,
    [库初数量]
    =case when b.数量<tmp.StocksSum+tmp.数量 then b.数量-tmp.StocksSum else tmp.数量 end,
    [库初数量]
    =case when b.数量<tmp.StocksSum+tmp.数量 then b.金额-tmp.StocksSum else tmp.金额 end
    from 
    (select *,StocksSum=
    isnull((select sum(case when [进出]=1 then [数量] else -[数量] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]>a.[凭证号]),0),
    StocksPrice=
    isnull((select sum(case when [进出]=1 then [金额] else -[金额] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]>a.[凭证号]),0)
    from @仓库凭证 as a )tmp 
    right join @库存 b  on tmp.仓库=b.仓库 and tmp.物品=b.物品
    and b.数量 !<tmp.StocksSum+tmp.数量 
    (3 行受影响)(1 行受影响)
    仓库          物品    凭证号  库初数量        库初数量
    ----------- ----- ---- ----------- ---------------------------------------
    1           B     001  100         1000.000
    1           B     002  40          400.000
    1           B     003  50          500.000(3 行受影响)
      

  5.   

    理解错误,好像这样就行了,如果凭证有大小关系时:
    declare @仓库凭证 table(仓库 int,凭证号 char(3) ,物品 varchar(5),进出 bit, 数量 int,金额 decimal(9,3))
    insert into @仓库凭证
    select '1','001','B',1,100,1000 union all
    select '1','002','B',0,40,400 union all
    select '1','003','B',1,50,500declare @库存 table (仓库 int,物品 varchar(5),数量 int,金额 decimal(9,3))
    insert into @库存
    select '1','B',110,1100select *
    from 
    (select *,[库初数量]=
    isnull((select sum(case when [进出]=1 then [数量] else -[数量] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]<a.[凭证号]),0),
    [库初金额]=
    isnull((select sum(case when [进出]=1 then [金额] else -[金额] end) 
    from @仓库凭证 where 仓库=a.仓库 and 物品=a.物品 and [凭证号]<a.[凭证号]),0)
    from @仓库凭证 as a )tmp 
    (3 行受影响)(1 行受影响)
    仓库          凭证号  物品    进出    数量          金额                                      库初数量        库初金额
    ----------- ---- ----- ----- ----------- --------------------------------------- ----------- ---------------------------------------
    1           001  B     1     100         1000.000                                0           0.000
    1           002  B     0     40          400.000                                 100         1000.000
    1           003  B     1     50          500.000                                 60          600.000(3 行受影响)