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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCCommented:
@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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.