如何创建一个查询,该查询将基于LoanApplicationDate返回最新的Soa LoanableAmount。
例如: 对于LoanID = 1,我想获取SoaID = 2的Soa记录,因为这是此LoanApplicationDate的最新可贷金额-2017-07-01。
到目前为止,这是我已经完成的事情:
select *
from Loan L
join Soa S
ON S.EmployeeID = L.EmployeeID
where S.TransactionDate <= L.LoanApplicationDate
为说明我要完成的工作,请参见下面的屏幕截图。
答案 0 :(得分:1)
我认为您需要为联接设置条件:
select
L.LoanID,
L.EmployeeID,
L.LoanAmount,
L.LoanApplicationDate,
S.LoanableAmount,
S.TransactionDate
from Loan L join Soa S
ON S.EmployeeID = L.EmployeeID and
S.TransactionDate = (select max(TransactionDate) from Soa where TransactionDate <= L.LoanApplicationDate)
请参见demo
答案 1 :(得分:1)
select *
from Loan L
join Soa S
ON S.EmployeeID = L.EmployeeID
and S.TransactionDate =
(select max(S2.TransactionDate)
from Soa S2
where S2.TransactionDate <= L.LoanApplicationDate
and S2.`EmployeeID` = L.`EmployeeID`
)
答案 2 :(得分:1)
我认为您需要doc
,然后进行过滤以获取最新日期:
join
答案 3 :(得分:0)
以下查询将为您提供所有雇员ID,并在最近日期提取可贷金额
Select employee_id,
LoanableAmount
transactionDate from table
having
transactionDate=max(transactionDate)
group by employee_id
答案 4 :(得分:0)
您需要为每行运行一个子查询:
select q.*, S.LoanableAmount, S.TransactionDate from
(select
L.*
,(select SoaID from Soa x where x.EmployeeID = L.EmployeeID and x.TransactionDate <= L.LoanApplicationDate order by TransactionDate DESC limit 1) as SoaID
from Loan L) q
join Soa S on S.SoaID = q.SoaID
第4行将为您提供Soa表中行的SoaID,该行的最新日期小于或等于“申请日期”,然后使用该ID进行联接并显示其余字段。