Sql Server版本的股票估价查询

时间:2013-02-08 18:49:08

标签: sql database sql-server-2008-r2

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

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

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

计算公式是     ((old_stock x旧单价)+(New_Purchase_qty x新单价))/(旧库存数量+新购买数量) 记录是

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

1 个答案:

答案 0 :(得分: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年。