Solved

vba Query to get the last record of each day in table.

Posted on 2013-01-29
5
503 Views
Last Modified: 2013-01-29
Hello all

I have a table that I need to have a query to get the last record for each day.

For sake of this example, I will keep it simple.

Let us call the table [dta]

Let us assume the fields are.
[ID]
[Info1]
[Info2]
[Info3]
[Date]

Now then I know how to use Dmax("ID","dta","Date") To get the last record.  However I need that Last record for each and every day...????????
0
Comment
Question by:wlwebb
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38833573
Select max(ID), [Date] from dta group by [date]
0
 
LVL 26

Assisted Solution

by:jerryb30
jerryb30 earned 150 total points
ID: 38833577
Select ID, info1, info2, info3, [date] from dta where id in(Select max(ID) from dta group by [date])
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 350 total points
ID: 38833585
I'm not clear whether you want:

a) The record with the highest [ID] value for each date (where the [Date] field contains only a date with no time part), OR

b) The record with the highest date/time value in the [Date] field for each date ONLY in the [Date] field (where the [Date] field contains BOTH a date part AND a time part)

I assume that [ID] contains a unique value.

This query should satisfy case (a) above:

SELECT * FROM dta 
    WHERE [ID]=(SELECT TOP 1 [ID] FROM dta as X 
         WHERE X.[Date]=dta.[Date] 
         ORDER BY [ID] desc);

Open in new window


This query should satisfy case (b):

SELECT * FROM dta 
    WHERE [ID]=(SELECT TOP 1 [ID] FROM dta as X 
         WHERE DateValue(X.[Date])=DateValue(dta.[Date]) 
         ORDER BY [Date] desc);

Open in new window

0
 

Author Comment

by:wlwebb
ID: 38833593
Thank you!
0
 

Author Closing Comment

by:wlwebb
ID: 38833613
GrahamMandeno THANK YOU for thinking past the original limited question!!!!!!! Much appreciated.
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

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…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

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

17 Experts available now in Live!

Get 1:1 Help Now