SELECT ReviewMain.LoanID AS [Loan ID], ReviewMain.Asofdate AS [As of Date],
(SELECT CreditRating FROM tblReviewScalars AS Review
WHERE (Review.AsOfDate = (SELECT Max(AsOfDate) FROM tblReviewScalars
WHERE AsOfDate<#4/19/2011#))
AND (Review.LoanID=ReviewMain.LoanID)
AND (Review.Asofdate=ReviewMain.Asofdate))
AS [CreditRatingAt prior AsOfDate],
FROM tblReportVectors AS ReportMain INNER JOIN
tblReviewScalars AS ReviewMain ON (ReportMain.LoanID = ReviewMain.LoanID)
AND (ReportMain.AsOfDate = ReviewMain.AsOfDate)
WHERE (ReviewMain.Asofdate= DateValue(FunAsofdate()))
GROUP BY ReviewMain.LoanID, ReviewMain.Asofdate;
在上面的查询中我有外部查询&amp;内部查询我得到了两个答案,但是当我在内部查询中写(Review.LoanID=ReviewMain.LoanID) AND (Review.Asofdate=ReviewMain.Asofdate)
时,我得到空白值,但是实际答案是信用是CCC-。是内部查询的任何解决方案。
答案 0 :(得分:0)
这个子查询:
(SELECT Max(AsOfDate) FROM tblReviewScalars
WHERE AsOfDate<#4/19/2011#)
在2011年4月19日之前为 所有 贷款返回最大AsOfDate,而不是当前行中贷款的最大AsOfDate。您需要将上面的子查询更改为:
(SELECT Max(AsOfDate) FROM tblReviewScalars AS MaxPrior
WHERE MaxPrior.AsOfDate<#4/19/2011#
AND MaxPrior.LoanID=ReviewMain.LoanID)
此外,您需要删除行AS [CreditRatingAt prior AsOfDate],
上的尾随逗号,以便在语法上正确识别SQL。