Solved

Query, return most recent payment and amount

Posted on 2014-04-01
7
259 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
  • 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

806 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