从 loanhist 获取最新的交易明细
Get latest transaction details from loanhist
我有一个贷款历史记录 table,其中包含客户借入和偿还的所有贷款。有两个状态(在同一行)表示借贷和还贷:LD = loan disburse while LP = Loan Paid.
我的问题是这样的,我正在寻找:
- 未结余额(LD中的总和(本金+利息)-LP中的总和(本金+利息))
- LD中的分期金额(sum(本金+利息))
- 所有 LD 的总和,即本金 + 利息作为贷款金额
- 最后还款日期,即在 LP
- LP中支付的金额(本金+利息)
Table结构
这是我目前的解决方案:
LP
$amtdues = "select h.ac_no, loan_amt, MAX(trx_date) AS Last_Pay, MAX(principal+interest) as Last_Paid, disb_date, exp_date, principal+interest as loan_interest, MAX(principal-interest) as outstanding_balance, trx_date as payment_date from loanhist h, loans l where h.ac_no = '$id' and l.ac_no = '$id' and trx_type = 'LP' group by trx_date, loan_amt, disb_date, exp_date, h.ac_no, interest, principal, trx_date ";
$amts = sqlsrv_query($conn, $amtdues);
$lp = sqlsrv_fetch_array($amts, SQLSRV_FETCH_ASSOC);
对于LD
$amtdues2 = "select h.ac_no, loan_amt, MAX(trx_date) AS Last_Pay, disb_date, exp_date, principal+interest as loan_interest, MAX(interest+principal) as outstanding_balance, MAX(interest+principal) as installment, trx_date as payment_date from loanhist h, loans l where h.ac_no = '$id' and l.ac_no = '$id' and trx_type = 'LD' group by trx_date, loan_amt, disb_date, exp_date, h.ac_no, interest, principal, trx_date ";
$amts2 = sqlsrv_query($conn, $amtdues2);
$ld = sqlsrv_fetch_array($amts2, SQLSRV_FETCH_ASSOC);
我的输出在html
<td><?php echo parseCurrency(abs($lmts['loan_limit']));?></td>
<td><?php echo parseCurrency(abs($ld['loan_interest']));?></td>
<td><?php echo parseCurrency(abs($ld['outstanding_balance'] - $lp['outstanding_balance']));?> </td>
<td><?php echo parseCurrency(abs($ld['installment']));?></td>
在尝试了这些查询之后,我得到了不正确的值。有没有姐妹帮帮忙啊?
基于 loanhist
的 table 信息并对 loans
中的内容做出一些假设(并且 loans
中存在 1:many 关系至 loanhist
),我认为此查询将使您朝着正确的方向前进。
SELECT l.ac_no,
l.loan_amt,
l.disb_date,
l.exp_date,
SUM(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) -
SUM(CASE WHEN trx_type='LP' THEN h.principal+h.interest ELSE 0 END) AS outstanding_balance,
MAX(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) AS instalment_amount,
SUM(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) AS loan_amount,
MAX(CASE WHEN trx_type='LP' THEN trx_date ELSE NULL END) AS last_payment_date,
SUM(CASE WHEN trx_type='LP' THEN h.principal+h.interest ELSE 0 END) AS amount_paid
FROM loans l
JOIN loanhist h
ON h.ac_no = l.ac_no
WHERE l.ac_no = '$id'
GROUP BY l.ac_no, l.loan_amt, l.disb_date, l.exp_date
我有一个贷款历史记录 table,其中包含客户借入和偿还的所有贷款。有两个状态(在同一行)表示借贷和还贷:LD = loan disburse while LP = Loan Paid.
我的问题是这样的,我正在寻找:
- 未结余额(LD中的总和(本金+利息)-LP中的总和(本金+利息))
- LD中的分期金额(sum(本金+利息))
- 所有 LD 的总和,即本金 + 利息作为贷款金额
- 最后还款日期,即在 LP
- LP中支付的金额(本金+利息)
Table结构
这是我目前的解决方案:
LP
$amtdues = "select h.ac_no, loan_amt, MAX(trx_date) AS Last_Pay, MAX(principal+interest) as Last_Paid, disb_date, exp_date, principal+interest as loan_interest, MAX(principal-interest) as outstanding_balance, trx_date as payment_date from loanhist h, loans l where h.ac_no = '$id' and l.ac_no = '$id' and trx_type = 'LP' group by trx_date, loan_amt, disb_date, exp_date, h.ac_no, interest, principal, trx_date ";
$amts = sqlsrv_query($conn, $amtdues);
$lp = sqlsrv_fetch_array($amts, SQLSRV_FETCH_ASSOC);
对于LD
$amtdues2 = "select h.ac_no, loan_amt, MAX(trx_date) AS Last_Pay, disb_date, exp_date, principal+interest as loan_interest, MAX(interest+principal) as outstanding_balance, MAX(interest+principal) as installment, trx_date as payment_date from loanhist h, loans l where h.ac_no = '$id' and l.ac_no = '$id' and trx_type = 'LD' group by trx_date, loan_amt, disb_date, exp_date, h.ac_no, interest, principal, trx_date ";
$amts2 = sqlsrv_query($conn, $amtdues2);
$ld = sqlsrv_fetch_array($amts2, SQLSRV_FETCH_ASSOC);
我的输出在html
<td><?php echo parseCurrency(abs($lmts['loan_limit']));?></td>
<td><?php echo parseCurrency(abs($ld['loan_interest']));?></td>
<td><?php echo parseCurrency(abs($ld['outstanding_balance'] - $lp['outstanding_balance']));?> </td>
<td><?php echo parseCurrency(abs($ld['installment']));?></td>
在尝试了这些查询之后,我得到了不正确的值。有没有姐妹帮帮忙啊?
基于 loanhist
的 table 信息并对 loans
中的内容做出一些假设(并且 loans
中存在 1:many 关系至 loanhist
),我认为此查询将使您朝着正确的方向前进。
SELECT l.ac_no,
l.loan_amt,
l.disb_date,
l.exp_date,
SUM(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) -
SUM(CASE WHEN trx_type='LP' THEN h.principal+h.interest ELSE 0 END) AS outstanding_balance,
MAX(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) AS instalment_amount,
SUM(CASE WHEN trx_type='LD' THEN h.principal+h.interest ELSE 0 END) AS loan_amount,
MAX(CASE WHEN trx_type='LP' THEN trx_date ELSE NULL END) AS last_payment_date,
SUM(CASE WHEN trx_type='LP' THEN h.principal+h.interest ELSE 0 END) AS amount_paid
FROM loans l
JOIN loanhist h
ON h.ac_no = l.ac_no
WHERE l.ac_no = '$id'
GROUP BY l.ac_no, l.loan_amt, l.disb_date, l.exp_date