SQL Server 2012:加权平均计算

SQL Server 2012: Weighted Average Calculation(SQL Server 2012:加权平均计算)
本文介绍了SQL Server 2012:加权平均计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试计算表格中某些数据的加权平均成熟度.

I'm trying to calculate the weighted average maturity of some data in my table.

SaleEventID  LID(PK)    CurrentUPB         Interest Rate    RemainingMonths
1            1          $100,000.00         6.100%           11.00
1            2          $67,000.00          6.200%           360.00
1            3          $1,400,000.00       6.300%           240.00
1            4          $500,000.00         7.000%           100.00
2            5          $1,400,000.00       7.100%           240.00
2            6          $500,000.00         7.000%           100.00

所以我想要完成的公式是 (WAM):
1) 乘以 CurrentUPB * RemainingMonths for LID=1
2) 对匹配 WHERE SaleEventID=1
的每一行执行此操作3) 对以上计算求和 = $411,220,000.00 = A
4) SUM 所有 CurrentUPB WHERE SaleEventID=1 等于 $2,067,000.00 =B
5) 然后除以 A/B = $198.95 这是我的 WAM

So the formula i'm trying to accomplish is (WAM):
1) Multiply CurrentUPB * RemainingMonths for LID=1
2) Do that for each row that matches WHERE SaleEventID=1
3) SUM the above calculation = $411,220,000.00 = A
4) SUM all the CurrentUPB WHERE SaleEventID=1 which equals $2,067,000.00 =B
5) Then Divide A/B = $198.95 which is my WAM

我需要考虑的是,在我的表中,我将有许多贷款,并且每个贷款都不会被赋予相同的 SaleEventID 值(不是主键)

I need to consider that in my table I will have many Loans and that each will not be attributed the same SaleEventID value (Which is not the Primary Key)

到目前为止我的查询:

SELECT l.*, A / B FROM AS WAM
FROM ( SELECT LSX_DC_Loans l
        (SELECT CurrentUPB * RemainingMonths FROM l WHERE LID = 1
         ) AS A
        (SELECT SUM (CurrentUPB) CurrentUPB FROM LSX_DC_Loans
        WHERE SaleEventID = 1
        ) AS B
FROM l
) l

我无法弄清楚如何执行第 2 步和;4. 任何帮助,示例高度赞赏.

I'm having trouble figuring out how to do steps 2 & 4. Any help, examples highly appreciated.

推荐答案

计算 WAM 和 WAIR 非常简单.想想 Excel 中的 sumproduct()

To calculate WAM and WAIR is pretty simple. Think sumproduct() in Excel

Declare @YourTable table (SaleEventID  int,LID int,CurrentUPB money,[Interest Rate] money,RemainingMonths money)
Insert Into @YourTable values
(1,1,100000.00,6.100, 11.00),
(1,2,67000.00,6.200, 360.00),
(1,3,1400000.00,6.300, 240.00),
(1,4,500000.00,7.000, 100.00),
(2,5,1400000.00,7.100, 240.00),
(2,6,500000.00,7.000,100.00)


Select SaleEventID
      ,UPB  = sum(CurrentUPB)
      ,WAM  = sum(CurrentUPB*RemainingMonths)/sum(CurrentUPB)
      ,WAIR = sum(CurrentUPB*[Interest Rate] )/sum(CurrentUPB)
 From  @YourTable
 Where SaleEventID = @Event
 Group By SaleEventID

退货

SaleEventID UPB         WAM         WAIR
1           2067000.00  198.9453    6.4564
2           1900000.00  203.1578    7.0736

这篇关于SQL Server 2012:加权平均计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)