I am using a subquery trying to grab the most recent data, but, having no luck in doing so.
Here is the normal query, that I am able to get the information: Select ccr.RateIndexRate + ccr.RateIndexMargin, Max(ccr.LastModifiedDate)
From CreditCardSplitRateDetail ccr
Where ccr.RateIndexID = 1 AND ccr.SR_RateTypeIndex = 0 AND ccr.SR_BalanceTypeIndex = 0
Group By ccr.RateIndexRate, ccr.RateIndexMargin
I need this in a subquery, but , can't seem to wrap my head on how to do it. (Select Case When Max(ccr.LastModifiedDate) Then Max(ccr.RateIndexRate + ccr.RateIndexRate) Else 0 End From CreditCardSplitRateDetail ccr Where ccr.MemberNumber = ln.MemberNumber AND ccr.LoanNumber = ln.LoanNumber AND ccr.RateIndexID = 1 AND ccr.SR_RateTypeIndex = 0 AND ccr.SR_BalanceTypeIndex = 0)
Select
Case When Max(ccr.LastModifiedDate)
Then Max(ccr.RateIndexRate + ccr.RateIndexRate)
Else 0 End
From CreditCardSplitRateDetail ccr
Where ccr.MemberNumber = ln.MemberNumber
AND ccr.LoanNumber = ln.LoanNumber
AND ccr.RateIndexID = 1
AND ccr.SR_RateTypeIndex = 0
AND ccr.SR_BalanceTypeIndex = 0
(Select Max(ccr.RateIndexRate + ccr.RateIndexMargin) From CreditCardSplitRateDetail ccr Where ccr.MemberNumber = ln.MemberNumber AND ccr.LoanNumber = ln.LoanNumber AND (ccr.RateIndexID = 1 OR ccr.RateIndexID = 8) AND ccr.SR_RateTypeIndex = 0 AND ccr.SR_BalanceTypeIndex = 0 AND ccr.LastModifiedDate = Max(ccr.LastModifiedDate))
I tried this, but got the same 'An Aggregate may not appear in the WHERE clause" message.
(Select Max(ccr.RateIndexRate)
From CreditCardSplitRateDetail ccr
Where ccr.MemberNumber = ln.MemberNumber
AND ccr.LoanNumber = ln.LoanNumber
AND (ccr.RateIndexID = 1 OR ccr.RateIndexID = 8)
AND ccr.SR_RateTypeIndex = 0
AND ccr.SR_BalanceTypeIndex = 0
AND ccr.LastModifiedDate = (select Max(ccr.LastModifiedDate) From CreditCardSplitRateDetail))
The desired result of the subquery is the most recent ccr.RateIndexRate.
(Select (ccr.RateIndexRate)
From CreditCardSplitRateDetail ccr
Where ccr.MemberNumber = ln.MemberNumber
AND ccr.LoanNumber = ln.LoanNumber
AND (ccr.RateIndexID = 1 OR ccr.RateIndexID = 8)
AND ccr.SR_RateTypeIndex = 0
AND ccr.SR_BalanceTypeIndex = 0
AND ccr.LastModifiedDate = (select Max(ccr.LastModifiedDate) From CreditCardSplitRateDetail))
Select ln.MemberNumber
, ln.LoanNumber
, ln.OpenDate
, ir.RateIndexRate
From Loan ln
Cross Apply (Select Top (1)
ccr.RateIndexRate
From CreditCardSplitRateDetail ccr
Where ccr.SR_RateTypeIndex = 0
And ccr.SR_BalanceTypeIndex = 0
And ccr.MemberNumber = ln.MemberNumber
And ccr.LoanNumber = ln.LoanNumber
And ccr.RateIndexID In (1, 8)
Order By
ccr.LastModifiedDate desc
) ir
Where ... -- something missing???
One way is the above cross apply which gets the latest value. With that said - you are not filtering or grouping so you should be able to do something like this:
Select ln.MemberNumber
, ln.LoanNumber
, ln.OpenDate
, RateIndexRate = first_value(ir.RateIndexRate) Over(Partition By ln.MemberNumber, ln.LoanNumber, ln.OpenDate Order By ln.LastModifiedDate desc)
From Loan ln
Where ... -- something missing???
May need to adjust the partition and/or use last_value.
Hey Jeff. First of all, thank you for your comprehensive response!
Looks like this did the trick as a subquery:
Rate_Index = (Select Top (1) ccr.RateIndexRate
From CreditCardSplitRateDetail ccr
Where ccr.SR_RateTypeIndex = 0
And ccr.SR_BalanceTypeIndex = 0
And ccr.MemberNumber = ln.MemberNumber
And ccr.LoanNumber = ln.LoanNumber
And ccr.RateIndexID In (1, 8)
Order By
ccr.LastModifiedDate desc
)