我有我的代碼問題,它似乎沒有接受我的選擇查詢中的聚合函數。我試圖用我的DATEDIFF(MONTH,MIN(LoanDateStart),MAX(LoanPaymentDue))
實現的是我想要獲得總月數,然後使用查詢餘下的月份來計算月數。如何在Select語句中使用Aggregate
我得到這個錯誤:
Msg 130, Level 15, State 1, Line 11 Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
反正是有,我可以圍繞實現其他的方式?
查詢
SELECT
ISNULL(SUM((CAST(((((lt.InterestRate/100) * lc.LoanAmount) +
lc.LoanAmount)/((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart,
101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * DATEDIFF(MONTH,
MIN(LoanDateStart), MAX(LoanPaymentDue))) * 2)) AS DECIMAL(18,2)))),0)
FROM LoanContract lc
INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID
WHERE lc.LoanTypeID = 1 AND lc.EmployeeID = 5
說明已添加到答案中。 – toonice
感謝這個偉大的解釋,我現在完全明白爲什麼。非常感謝你@toonice –
不客氣。很高興有幫助。 – toonice