Solved

Access 2010 Query to locate most recent item only, when 1 is expected each month

Posted on 2014-01-02
9
289 Views
Last Modified: 2014-01-02
I receive statements each month and I am trying to determine if a statement did not come in.  Example: account 12345 last statement was November 2013, so I am missing a December statement.  

I am trying to locate the newest statement received only but my query gives me all of the statement dates, not just the newest.

My Fields:
AccountNumber
StatementDate
ClientName
0
Comment
Question by:NNiicckk
  • 3
  • 3
  • 3
9 Comments
 
LVL 6

Expert Comment

by:ButlerTechnology
ID: 39751210
Try this:
Select * 
from table a
where StatementDate = (Select max(Statmentdate) from table b where accountnumber = a.accountnumber)

Open in new window


The results should be the last statement for each customer.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39751253
try this


select a.*
from NameOfTable as a
inner join (select max(b.StatementDate) as MaxDate, b.AccountNumber
           from NameOftable as b
           group by b.AccountNumber ) as c
on a.StatementDate=c.maxDate and  a.AccountNumber =  c.AccountNumber
0
 

Author Comment

by:NNiicckk
ID: 39751347
Sorry with regard to field name sin the query,

The fields AccountNumber and Client Name come from the table Accounts.  The field StatementDate comes from the table Statements.
0
 
LVL 6

Expert Comment

by:ButlerTechnology
ID: 39751359
Try this one with the new table names:
Select *
from Accounts a inner join Statements S on a.accountNumber = s.AccountNumber
where s.statementDate = (Select max(StatementDate) from statements where accountNumber = a.Accountnumber

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39751424
try this


select a.*
from Accounts as a
inner join (select max(b.StatementDate) as MaxDate, b.AccountNumber
           from Statements as b
           group by b.AccountNumber ) as c
on a.AccountNumber =  c.AccountNumber
0
 

Author Comment

by:NNiicckk
ID: 39751503
The max(StatementDate) gives me the max statement date in the system. Example: 12/20

I need the max statement date per each account #.

This is my SQL:

SELECT Accounts.AccountNumber AS Account, Statements.StatementDate AS [Last Statement], Accounts.ClientName AS Client, Accounts.Payee, Accounts.Status
FROM Accounts INNER JOIN Statements ON Accounts.[AccountNumber] = Statements.[AccountID]
WHERE Statements.[StatementDate] = (Select max(StatementDate) FROM Statements where AccountNumber = Accounts.AccountNumber)


EXAMPLE OF OUTPUT:  This would show me missing 2 December statements

Account      Last Statement      Client      Payee      Status
12345      11-Nov-13      aaaa      10101      Active
45678      30-Nov-13      bbbb      10102      Active
159623      15-Dec-13      cccc      10103      Closed
111422      30-Dec-13      dddd      10104      Active
0
 
LVL 6

Accepted Solution

by:
ButlerTechnology earned 250 total points
ID: 39751514
I think you nee to make a minor change to sub query:
(Select max(StatementDate) FROM Statements where AccountID= Accounts.AccountNumber)

Open in new window

instead of accountnumber = accountnumber
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 39751592
try this


select a.AccountNumber as Account, c.MaxDate as [Last Statement], a.ClientName AS Client, a.Payee, a.Status
from Accounts as a
inner join (select max(b.StatementDate) as MaxDate, b.AccountID
           from Statements as b
           group by b.AccountID ) as c
on a.AccountNumber =  c.AccountID
0
 

Author Closing Comment

by:NNiicckk
ID: 39751858
Thank 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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
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 …

910 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

22 Experts available now in Live!

Get 1:1 Help Now