473,379 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Getting a specific record by date field

69
Hello all...

I'm having a problem getting a query to work. As a matter of fact I can't even seem to get a good start.

I have a table with the following example data

Expand|Select|Wrap|Line Numbers
  1. ID#     PRE_ID#     POST_ID#     DATE
  2. 1          1           2         1/1/2001
  3. 1          1           2         1/1/2004
  4. 1          1           2         1/1/2006     *
  5. 1          3           4         1/1/2004
  6. 1          3           4         1/1/2005
What I want to do is find the latest date (in this example 1/1/2006), and then see if the ID# is equal to the PRE_ID#. If it is I want to select that record. So in this example I would select the record that I've marked with a *.

But if my data looked like this:

Expand|Select|Wrap|Line Numbers
  1. ID#     PRE_ID#     POST_ID#     DATE
  2. 1          1           2         1/1/2001
  3. 1          1           2         1/1/2004
  4. 1          1           2         1/1/2003     
  5. 1          3           4         1/1/2004
  6. 1          3           4         1/1/2005
I would not select any record, because the record with the latest date (1/1/2005 in this example) does not have a matching ID# and PRE_ID#.

If anyone knows how I could write this query I would really appreciate it.

Thanks
Rod
May 7 '07 #1
8 1933
MMcCarthy
14,534 Expert Mod 8TB
Hi Rod

Try something like this ...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE [ID#]=[PRE_ID#]
  3. AND Max([Date]) = DMAX("[Date]", "TableName")
  4.  
Mary
May 7 '07 #2
narpet
69
Thanks for the info.

When I enter this query exactly as you've given it to me (replacing names appropriately of course), and I run it, I get the error "Cannot have aggregate function in WHERE clause".

Any ideas?
May 7 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Sorry try this ...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE [ID#]=[PRE_ID#]
  3. AND [Date] = DMAX("[Date]", "TableName")
  4.  
Mary
May 7 '07 #4
narpet
69
First off... I really appreciate your help very much. We are getting closer.

Okay, now the query is working but it looks like it's giving me the absolute latest date over all records, so it's only pulling one record.

In the example above there can be many ID#s, PRE_ID#s and POST_ID#s. For any given ID# (of which there are thousands) I need to check the criteria noted above and see if I should select that ID# record.

So, I should actually get several (in my case tens to hundreds) records. This query seems to be finding the absolute latest date in the entire table and giving me that record (where the ID# and PRE_ID# also match).

Is there any way to do this so that all ID#s that match the above criteria will be selected?

Thanks so much for your help!
Rod
May 7 '07 #5
JConsulting
603 Expert 512MB
Sorry try this ...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE [ID#]=[PRE_ID#]
  3. AND [Date] = DMAX("[Date]", "TableName")
  4.  
Mary

Option 6? :o)

SELECT *
FROM tblTest
WHERE date1=(select max([Date1]) from tblTest where [Range1]=[Range2]);
May 7 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
Try this and see what results you get ...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE [ID#]=[PRE_ID#]
  3. AND [Date] = DMAX("[Date]", "TableName", "[ID#]=" & [ID#])
  4.  
Mary
May 7 '07 #7
narpet
69
Try this and see what results you get ...
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE [ID#]=[PRE_ID#]
  3. AND [Date] = DMAX("[Date]", "TableName", "[ID#]=" & [ID#])
  4.  
Mary
This appears to have been exactly what I needed. Of course, with thousands of records I have to do quite a bit of manual confirmation before I can be sure.

Thanks so much for your help, everyone!
Rod
May 7 '07 #8
MMcCarthy
14,534 Expert Mod 8TB
This appears to have been exactly what I needed. Of course, with thousands of records I have to do quite a bit of manual confirmation before I can be sure.

Thanks so much for your help, everyone!
Rod
You're welcome.
May 7 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
5
by: deko | last post by:
I have a subform datasheet that contains a full year of records sorted by a date field. I'm trying to programmatically move the record selector on the datasheet to the first record that matches a...
11
by: David Messner | last post by:
Ok I know this is simple but the statement eludes me... I have a date field where I want the default value on the data entry form's date field to be the last date entered. I figure I can do this...
4
by: the hotshot | last post by:
hello, this seems to be a hard question so far and noone has been able to help with this. is it possible to have access start an autonumber with a prefix according to the year when the data is...
1
by: kkrizl | last post by:
I have a form that displays general information about an alarm permit location. There's a subform that shows detailed information about burglar alarms that have gone off at the location. When a...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
6
by: AA Arens | last post by:
Hi, I have a database with 2 main forms. Contacts and companies. I share the base with two others via LAN. On the companies form I have buttons to navigate throught the records (>400). We are...
7
by: lmnorms1 | last post by:
Hello, I am trying to update an access database record date field that matches a specific date. The code is not working. Anyone have any advice? Here is the code: Dim gConnString As String =...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.