1

我想计算平均股票价格并得到以下解决方案 Inventory Average Cost Calculation in SQL

这个查询看起来很棒,因为它正是我需要的

但我使用的是 SQL Server 2008。这个查询的 Sql Server 版本是什么?

计算公式为 ((old_stock x old unit price)+(New_Purchase_qty x New unit price))/(old stock qty + new purchase qty) 记录为

TrancDate   ProID   Qty CostRate    TrancType  closingStock    closingCostRate
02/06/2013  1   10.000  2.00               P             10                2
02/08/2013  1   7.000   0.00               S             3                 2
02/15/2013  1   15.000  3.00               P             18                2.83
02/16/2013  1   8.000   0.00               S             10                2.83
02/25/2013  1   20.000  4.00               P             30                3.61 
02/26/2013  1   9.000   0.00               S             21                3.61
02/26/2013  1   1.000   0.00               S             20                3.61
4

1 回答 1

0

如果计算是我认为的那样:

select t.*, cumetot / cumeqty
from (select t.*,
             (select SUM(qty)
              from t t2
              where t2.tranctype = 'P' and t2.proid = t.proid and t2.trancdate <= t.trancdate
             ) as cumeqty,
             (select SUM(qty*ClosingCostRate)
              from t t2
              where t2.TrancType = 'P' and t2.proid = t.proid and t2.trancdate <= t.trancdate
             ) as cumetot
      from t
     ) t

SQL Server 2012 具有累积总和和计数,但没有 2008。

于 2013-02-08T19:19:12.387 回答