店面库的库存存放在StoreFront表中,备用库的库存存放在StoreBack表中。表结构均如下:
SN(主键) ProductSN(产品) Qty(数量) PresellQty预售) Costing(进价) ProductID(批号)
1 1001(板蓝根) 10 3 20.00 200911
2 1002(感康) 21 2 11.20 200901
3 1003(斯达舒) 28 6 22.11 090901
4 1002(感康) 33 7 12.30 090801
5 1001(板蓝根) 75 0 45.01 080921(1).创建一个函数,输入参数:要查询库存的表名称(StoreName)和产品关键字,返回这个产品的在这个仓库的总库存。(2).创建一个函数,输入参数:要查询库存的表名称(StoreName)和产品关键字,返回这个产品的在这个仓库的平均进价。(3).创建一个表值函数,输入参数:要查询库存的表名称(StoreName) 和产品关键字,返回这个表的可出库库存明细信息(可用库存 = 库存 – 预售).

解决方案 »

  1.   

    --1
    if object_id('fun_amm')is not null drop function fun_amm
    go
    create function fun_amm
    (@ProductSN nvarchar(20))
     returns table
     as return
    (select ProductSN,sum(Qty) Qty 
     from [tb] 
     where ProductSN=@ProductSN
     group by ProductSN)
      

  2.   

    --2
    if object_id('fun_amm')is not null drop function fun_amm
    go
    create function fun_amm
    (@ProductSN nvarchar(20))
     returns table
     as return
    (select ProductSN,avg(Costing)Costing 
     from [tb] 
     where ProductSN=@ProductSN
     group by ProductSN)
      

  3.   

    1.
    select
     ProductSN,sum(Qty) Qty 
    from
     [tb] 
    where
     ProductSN=@ProductSN
    group by
     ProductSN
      

  4.   

    --3
    if object_id('fun_amm')is not null drop function fun_amm
    go
    create function fun_amm
    (@ProductSN nvarchar(20))
     returns table
     as return
    (select ProductSN,sum(Qty-PresellQty)[Qty-PresellQty] 
     from [tb] 
     where ProductSN=@ProductSN
     group by ProductSN)
      

  5.   

    --2
    select
     ProductSN,avg(Costing) as Costing 
    from
     [tb] 
    where
     ProductSN=@ProductSN
    group by
     ProductSN
      

  6.   

    ---3.
    select
     ProductSN,sum(Qty-PresellQty) as 'Qty-PresellQty' 
    from
     tb 
    where
     ProductSN=@ProductSN
    group by
     ProductSN