我试图计算表格中某些数据的加权平均成熟度。
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)乘以当前UPB *剩余月份LID = 1
2)对于匹配WHERE SaleEventID = 1的每一行执行此操作
3)SUM上述计算= $411,220,000.00
= A
4)SUM所有CurrentUPB WHERE SaleEventID = 1,等于$2,067,000.00
= B
5)然后除以A / B = $198.95
这是我的WAM
我需要考虑在我的表中我会有很多贷款,并且每个贷款都不会被归为相同的SaleEventID
值(这不是主键)
到目前为止我的查询:
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步和第2步。 4.任何帮助,高度赞赏的例子。
答案 0 :(得分:4)
计算WAM和WAIR非常简单。想想Excel中的sumproduct()
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
答案 1 :(得分:0)
如果我理解你的问题,这就是答案
SELECT
l.*
, l1.A / l2.B AS WAM
FROM
LXC_DC_Loans l
CROSS APPLY (SELECT SUM(CurrentUPB * RemainingMonths) AS A FROM LSX_DC_Loans WHERE SaleEventID=l.SaleEventID) l1
CROSS APPLY (SELECT SUM(CurrentUPB) AS B A FROM LSX_DC_Loans WHERE SaleEventID=l.SaleEventID) l2