我試圖計算表中某些數據的加權平均成熟度。SQL Server 2012:加權平均計算
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 *爲LID RemainingMonths = 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 & 4.任何幫助,例子高度讚賞。
什麼是洛納? – DVT
剛剛更新了錯過輸入網格中的信息的問題。 – UserSN
$ 411,220,000/$ 3,967,000 = $ 103.6602健康198.95從何而來? – xQbert