我的表格如下:
类别     名称    规格    单位    出库数量    入库数量   出入库单价    存放区域
电脑     键盘    1       个      0           10         100            一楼
电脑     键盘    1       个      6           0          600            一楼
日用品   椅子    2x4     把      0           5          10             二楼要实现下面的统计结果
类别     名称    规格    单位    当前库存量   当前产品库存价值
电脑     键盘    1       个      4            40
日用品   椅子    2x4     把      5            50上面统计结果里的键盘库存量的价值是库存量4乘以“出入库单价”列里的入库单价100来获得的,请问如何书写select查询使得分组统计的时候其单价是按该产品入库时的单价来计算?

解决方案 »

  1.   

    What happens if 入库单价 is different between different entries?At the end of the day, what it really boils down to is what costing methods you are attempting to use:Is it Average Costing - Unit Cost is continually updated whenever there is movement of stockOr is it First-in-First-Out (FIFO) - This is assuming products going in first are consumed firstOr is it Last-in-First-Out (LIFO) - Reverse of FIFOOr Specific Identification - Each product going out can be matched to the initial incoming batch and hence specific cost can be attributed to the outgoing batch. This would also mean that you could identify remaning Stock on Hand (SOH) and their specific cost.Given your data structure, it does not look like Specific Identification is possible unless you have omitted a few columns (such as batch number field). Hence please advise what exactly is the costing method you wish to use to calculate your cost.
      

  2.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([类别] varchar(6),[名称] varchar(4),[规格] varchar(3),[单位] varchar(2),[出库数量] int,[入库数量] int,[出入库单价] int,[存放区域] varchar(4))
    insert [tb]
    select '电脑','键盘','1','个',0,10,100,'一楼' union all
    select '电脑','键盘','1','个',6,0,600,'一楼' union all
    select '日用品','椅子','2x4','把',0,5,10,'二楼'--------------------------------查询开始------------------------------select 类别,名称,规格,单位,
    sum(入库数量-出库数量) as 当前库存量,
    sum(入库数量-出库数量)*(select 出入库单价 from [tb] where 出库数量=0 and 类别=a.类别 and [名称]=a.[名称]) as 当前产品库存价值
    from [tb] a
    group by 类别,名称,规格,单位
    /*
    类别     名称   规格   单位   当前库存量       当前产品库存价值
    ------ ---- ---- ---- ----------- -----------
    电脑     键盘   1    个    4           400
    日用品    椅子   2x4  把    5           50(2 行受影响)
    */
      

  3.   


    select 类别,
     名称,
     规格,
     单位,
     当前库存量=sum(出库数量)-sum(入库数量),
     当前产品库存价值=--这个条件不足,很难做
    from 表
    group by 类别,
     名称,
     规格,
     单位,
      

  4.   

    如果有多次入库,取平均值
    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb](
    [类别] varchar(6),
    [名称] varchar(4),
    [规格] varchar(3),
    [单位] varchar(2),
    [出库数量] int,
    [入库数量] int,
    [出入库单价] int,
    [存放区域] varchar(4)
    )
    insert [tb]
    select '电脑','键盘','1','个',0,10,100,'一楼' union all
    select '电脑','键盘','1','个',6,0,600,'一楼' union all
    select '日用品','椅子','2x4','把',0,5,10,'二楼'
    go---查询---
    select 类别,名称,规格,单位,
    sum(入库数量)-sum(出库数量) as 当前库存量, 
    (sum(入库数量)-sum(出库数量))
    *(sum(case when 出库数量=0 then 出入库单价 else 0 end)/sum(case when 出库数量=0 then 1 else 0 end)) as 当前产品库存价值
    from tb
    group by 类别,名称,规格,单位---结果---
    类别     名称   规格   单位   当前库存量       当前产品库存价值
    ------ ---- ---- ---- ----------- -----------
    电脑     键盘   1    个    4           400
    日用品    椅子   2x4  把    5           50(2 行受影响)
      

  5.   

    有项目开发管理经验的.NET的朋友加上限500人的QQ群28720769一起交流。