Solved

Query, return most recent payment and amount

Posted on 2014-04-01
7
266 Views
Last Modified: 2014-04-04
I have a payment file (tblPayFile) in my app.  There can be multiple payments for an account.  I would like to develop a query that returns me the most recent payment, based on pay date for every account along with the date and amount of the payment.  The three pertinent fields in the table are:

Account
PayDate
PayAmt
0
Comment
Question by:mlcktmguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 39970960
Try this:

SELECT Account, Max(PayDate) AS MostRecent
FROM tblPayFile
GROUP BY Account

Just create a new query and paste it into the sql.

Flyster
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 39970980
That doesn't get the amount of the payment
0
 
LVL 22

Expert Comment

by:Flyster
ID: 39971046
Add it to the select line:

SELECT Account, PayAmt, Max(PayDate) AS MostRecent
FROM tblPayFile
GROUP BY Account
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Author Comment

by:mlcktmguy
ID: 39971125
That causes an error, PayAmt has to also be in an aggregate function also.
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 225 total points
ID: 39971345
Try this:

SELECT  Account ,
        PayAmount ,
        PayDate
FROM    Account
WHERE   PayDate IN ( SELECT MAX(PayDate)
                     FROM   Account
                     GROUP BY Account )
ORDER BY Account

Open in new window

0
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 25 total points
ID: 39971393
No points for this. I believe Sreedhar Vengala gave you the correct solution. Here it is with your table structure:

SELECT tblPayFile.Account, tblPayFile.PayAmt, tblPayFile.PayDate
FROM tblPayFile
WHERE (((tblPayFile.[PayDate]) In (SELECT MAX(PayDate)
                     FROM   tblPayFile
                     GROUP BY Account )))
ORDER BY tblPayFile.Account;

Open in new window

0
 
LVL 1

Author Closing Comment

by:mlcktmguy
ID: 39977886
Thanks you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question