Avatar of NNiicckk
NNiicckk
 asked on

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

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
Microsoft Access

Avatar of undefined
Last Comment
NNiicckk

8/22/2022 - Mon
ButlerTechnology

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.
Rey Obrero (Capricorn1)

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
NNiicckk

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ButlerTechnology

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

Rey Obrero (Capricorn1)

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
NNiicckk

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
ButlerTechnology

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NNiicckk

ASKER
Thank You.