-1

Need help in generating report from DB getting Distinct value of Balance and getting the only oldest payment date per month (or not displaying the latest payment date per month)

current report

Sample below is the should-be report. I only deleted the latest rows to make a sample of my objective.

to-be report

here's the code:

SELECT payment_date,transaction_code,amount,interest_amount,
balance‌​,source FROM m_loan_payment 
WHERE loan_no = '13492' ORDER BY payment_date DESC
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

Here is one method, assuming the date is stored as a date:

SELECT lp.*
FROM m_loan_payment lp
WHERE loan_no = 13492 AND
      lp.payment_date = (SELECT MIN(lp2.payment_date)
                         FROM m_loan_payment lp2
                         WHERE lp2.loan_no = lp.loan_no AND
                               YEAR(lp2.payment_date) = YEAR(lp.payment_date) AND
                               MONTH(lp2.payment_date) = MONTH(lp.payment_date) 
                        )
ORDER BY payment_date DESC;

Similar logic can be used if the payment date is stored as a string or a number.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786