Solved

Query, return most recent payment and amount

Posted on 2014-04-01
7
258 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now