Solved

Query, return most recent payment and amount

Posted on 2014-04-01
7
257 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

705 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

19 Experts available now in Live!

Get 1:1 Help Now