Solved

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

Posted on 2013-01-29
5
490 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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 the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

12 Experts available now in Live!

Get 1:1 Help Now