Solved

Query To pull most recent payment and amount

Posted on 2015-01-21
9
104 Views
Last Modified: 2015-01-21
I know I can do this in code but I'm guessing there is a way to do it with a query, or 2.

These are the pertinent fields in my payment table:
ID                              :AutoNumber
TaxRecID                  :Id of the tax record payment is for.  There may be multiple, or no payments for a given tax record.
PaymentDate
PaymentAmount
PostedStatus           :2 = posted.  Posted payments are the only one's to be considered.

For any taxrecid's that have a posted payment, list the most recent posted payment, paymentdate and paymentamount.   At most one payment should be shown for each taxrecid.

If there are no payments for a taxrecID te taxrecID does not have to be in the query results.
0
Comment
Question by:mlcktmguy
  • 5
  • 4
9 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40562935
select a.TaxRecID, a.PaymentDate, a.PaymentAmount
from tablex as a inner join
(select  b.TaxRecID, max(b.PaymentDate) as maxPayDate
  from tablex as b
  group by b.taxRecId) as c
on a.TaxRecID= c.TaxRecID and a.PaymentDate = c.maxPayDate
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40562985
Thank you.  I don't see a qualifier to only include posted payments.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40563025
what do you mean? are you referring to PostedStatus?



select a.TaxRecID, a.PaymentDate, a.PaymentAmount, a.PostedStatus
from tablex as a inner join
(select  b.TaxRecID, max(b.PaymentDate) as maxPayDate
  from tablex as b
  group by b.taxRecId) as c
on a.TaxRecID= c.TaxRecID and a.PaymentDate = c.maxPayDate
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40563034
Yes, only posted payment should be considered in the selection.  There are also 'NSF' and 'Unposted' payments in the table.  We don't want those included.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40563061
ok. give a detail info about your table. not like this piece by piece of information
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40563064
post a sample expected result coming from your table.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40563085
The detail example of my table is in the original post:

"These are the pertinent fields in my payment table:
 ID                              :AutoNumber
 TaxRecID                  :Id of the tax record payment is for.  There may be multiple, or no payments for a given tax record.
 PaymentDate
 PaymentAmount
 PostedStatus           :2 = posted.  Posted payments are the only one's to be considered.

 For any taxrecid's that have a posted payment, list the most recent posted payment, paymentdate and paymentamount.   At most one payment should be shown for each taxrecid.

 If there are no payments for a taxrecID te taxrecID does not have to be in the query results. "
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40563093
post the result of this query

select a.TaxRecID, a.PaymentDate, a.PaymentAmount, a.PostedStatus
from tablex as a inner join
(select  b.TaxRecID, max(b.PaymentDate) as maxPayDate
  from tablex as b
  group by b.taxRecId) as c
on a.TaxRecID= c.TaxRecID and a.PaymentDate = c.maxPayDate
where a.PostedStatus=2
0
 
LVL 1

Author Closing Comment

by:mlcktmguy
ID: 40563159
Thank you, works correctly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

863 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

20 Experts available now in Live!

Get 1:1 Help Now