How to Pull Most Recent Payment Date and Amount For Each Customer

I have a Payment file in my Access 2013 application.  
The pertinent fields are

CustID                      Long
DepositDate            Date
PaymentAmount    Double

There may be many payments for each customer
There may be many payments with the same deposit date for the same customer.

I am trying to write a query that will list the most recent deposit date and payment amount on that deposit date for every client.

If there are multiple payments on the most recent deposit date, whichever payment amount, from that deposit date, that is pulled doesn't matter.

Only one line for every client.

Client            Most Recent Deposit Date           Payment Amount
123                       10/12/17                                      $320.23
124                       12/01/17                                     $12.34
125                       09/13/17                                     $567.89

etc..
LVL 1
mlcktmguyAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
if there's an ID (like autonumber) in your table and you wish to get latest amount as per CustomerID + DepositDate, then you can try like:

select 
tbl.BRT, tbl.DepositDate, tbl.PayAmt
from
(
SELECT  a.BRT, a.DepositDate, a.PayAmt, (SELECT COUNT(*) FROM tblPayments_Hdr WHERE a.ID <= ID and a.BRT = BRT ) as idx
FROM tblPayments_Hdr AS a
) as tbl
where tbl.idx = 1

Open in new window

0
 
Ryan ChongCommented:
try something like this:

select a.*
from Payment as a
inner join 
(
select Payment.CustID, Max(Payment.DepositDate) as MaxDepositDate
from Payment
group by Payment.CustID
) as b
on a.CustID = b.CustID and a.DepositDate = b.MaxDepositDate

Open in new window

1
 
mlcktmguyAuthor Commented:
Thanks for your suggestion but it does not return only one line for a customer that has multiple payments on the same deposit date.

If a customer has multiple payments on its most recent deposit date, the query includes every payment for that client on the deposit date.

The panel on the left is a query using your query as a base to show only BRT/CustID, Deposit Date and PayAmt.

The panel on the right is the actual data for that BRT/CustID in the table
 QueryResult
This is the query as I modified with the correct field and table names that I found

SELECT a.BRT, a.DepositDate, a.PayAmt
FROM tblPayments_Hdr AS a INNER JOIN (SELECT tblPayments_Hdr.BRT, Max(tblPayments_Hdr.DepositDate) AS MaxDepositDate FROM tblPayments_Hdr GROUP BY tblPayments_Hdr.BRT)  AS b ON (a.DepositDate = b.MaxDepositDate) AND (a.BRT = b.BRT)
ORDER BY a.BRT, a.DepositDate;

Open in new window


Any suggestions?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If you don't care which Payment Amount your get for a CustomerID + DepositDate

SELECT CustomerID, MAX(PaymentAmount) AS PaymentAmt, MAX(DepositDate) AS DepositDte FROM tblPayments_Hdr GROUP BY CustomerID

Or you could SUM the PaymentAmount instead, if that makes more sense
0
 
Dale FyeCommented:
@mlcktmguy,

You have encountered the reason that I use Now() and a full datetime value for most of my date fields.  With a date/time value in your DepositDate field, you can perform this process properly.  Or if you have an autonumber field associated with the record, you could use that too (although autonumbers are not guaranteed to be 'always increasing'), although this is not fool proof.

Your best bet is to change the DepositDate to a datetime and start using Now() to populate that field, rather than Date().

HTH
Dale
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.