?
Solved

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

Posted on 2013-01-29
5
Medium Priority
?
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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 1400 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

764 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