473,473 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access 2003 - Possible Subquery Problem

7 New Member
I am trying to create a query within a query in which there are more than one foreign keys, and within each set of foreign keys, there is more than one date. I want to retrieve the latest date for each foreign key and then I want to delete the remaining dates. I don't want to delete the record of the duplicated foreign key, just the older dates. How do I go about comparing the dates, and when I do, having it where only the latest date is remaining? The end result should be that for each set of foreign keys, there should only be one date.

i.e.,

FK Date

1 1/1/2003
1 2/21/2004
1 3/14/2005
2 10/19/2003
2 12/1/2003
2 5/6/2007

Result should look like:

FK Date

1
1
1 3/14/2005
2
2
2 5/6/2007


Thank you.

KOelschlaeger
May 7 '07 #1
13 1866
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE QueryName
  2. SET [Date]=Null
  3. WHERE [Date] < DMax("[Date]","QueryName","[FK]=" & [FK])
  4.  
May 8 '07 #2
KOelschlaeger
7 New Member
The suggested code seems to be deleting other columns than the specified Date column. It also seems to delete the date I would like to keep in the specified Date column. Here is an example of what I'm working with (The TTID is the primary key): [What I would like for it to look like is below this example].

FdrID NegDate NegFinishDate NegMiles TTID StartDate
3 1/1/2006 3/31/2006 35.12 6960 4/1/2006
3 1/1/2006 3/31/2006 35.12 2430 1/12/2002
3 1/1/2006 3/31/2006 35.12 121 1/9/1999
4 1/1/2006 3/31/2006 17.6 6861 3/18/2006
4 1/1/2006 3/31/2006 17.6 2431 2/23/2002
4 1/1/2006 3/31/2006 17.6 122 12/12/1998
5 1/1/2006 3/31/2006 18.25 6860 3/4/2006
5 1/1/2006 3/31/2006 18.25 2432 3/16/2002
5 1/1/2006 3/31/2006 18.25 123 11/14/1998

FdrID NegDate NegFinishDate NegMiles TTID StartDate
3 1/1/2006 3/31/2006 35.12 6960 4/1/2006
3 2430 1/12/2002
3 121 1/9/1999
4 1/1/2006 3/31/2006 17.6 6861 3/18/2006
4 2431 2/23/2002
4 122 12/12/1998
5 1/1/2006 3/31/2006 18.25 6860 3/4/2006
5 2432 3/16/2002
5 123 11/14/1998

Thank you.


Try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE QueryName
  2. SET [Date]=Null
  3. WHERE [Date] < DMax("[Date]","QueryName","[FK]=" & [FK])
  4.  
May 8 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Is this the query you are running?
Expand|Select|Wrap|Line Numbers
  1. UPDATE QueryName
  2. SET [NegDate]=Null,  [NegFinishDate]=Null
  3. WHERE [NegDate] < DMax("[NegDate]","QueryName","[ FdrID]=" & [ FdrID])
  4.  
May 8 '07 #4
KOelschlaeger
7 New Member
Actually, it's something like this:

UPDATE qryNegotiatedDates
SET qryNegotiatedDates.NegDate = Null, qryNegotiatedDates.NegMiles=Null, qryNegotiatedDates.NegFinishDate=Null
WHERE (((qryNegotiatedDates.StartDate)<DMax("StartDate", "qryNegotiatedDates","FdrID=" & "FdrID")));

I'm basing the date in which to determine which Negotiated information to keep on the latest Start Date information. Is this incorrect, or how should I go about determining a solution?



Is this the query you are running?
Expand|Select|Wrap|Line Numbers
  1. UPDATE QueryName
  2. SET [NegDate]=Null,  [NegFinishDate]=Null
  3. WHERE [NegDate] < DMax("[NegDate]","QueryName","[ FdrID]=" & [ FdrID])
  4.  
May 8 '07 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE qryNegotiatedDates 
  2. SET qryNegotiatedDates.NegDate = Null, qryNegotiatedDates.NegMiles=Null, qryNegotiatedDates.NegFinishDate=Null
  3. WHERE (((qryNegotiatedDates.StartDate)<DMax("[StartDate]","qryNegotiatedDates","[FdrID]=" & [FdrID])));
  4.  
May 8 '07 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
Just thinking, is FdrID a text field?
May 8 '07 #7
KOelschlaeger
7 New Member
Okay. I basically copied and pasted what you have and ALL the information went blank. I've even tried it when you place "NegDate" in place of "StartDate" to see if it makes a difference. It doesn't. I'm trying to avoid doing this by hand, and it seems like the problem shouldn't be too difficult to solve. However, I guess it's more complicated than I had originally figured.

Thank you.


Try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE qryNegotiatedDates 
  2. SET qryNegotiatedDates.NegDate = Null, qryNegotiatedDates.NegMiles=Null, qryNegotiatedDates.NegFinishDate=Null
  3. WHERE (((qryNegotiatedDates.StartDate)<DMax("[StartDate]","qryNegotiatedDates","[FdrID]=" & "[FdrID]")));
  4.  
May 8 '07 #8
KOelschlaeger
7 New Member
Yes, FdrID is a text field.

Just thinking, is FdrID a text field?
May 8 '07 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
OK, try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE qryNegotiatedDates 
  2. SET qryNegotiatedDates.NegDate = Null, qryNegotiatedDates.NegMiles=Null, qryNegotiatedDates.NegFinishDate=Null
  3. WHERE (((qryNegotiatedDates.StartDate)<DMax("[StartDate]","qryNegotiatedDates","[FdrID]='" & [FdrID] & "'")));
  4.  
May 8 '07 #10
KOelschlaeger
7 New Member
I'm having trouble understanding the last part, do you mean to have quotes around the second [FdrID]? It's giving me a "Data type mismatch in critera expression" when I copy what you have and when I place quotes around [FdrID]. What does the single quotation do so that I may fully understand the logic.

Thank you.

OK, try this ...
Expand|Select|Wrap|Line Numbers
  1. UPDATE qryNegotiatedDates 
  2. SET qryNegotiatedDates.NegDate = Null, qryNegotiatedDates.NegMiles=Null, qryNegotiatedDates.NegFinishDate=Null
  3. WHERE (((qryNegotiatedDates.StartDate)<DMax("[StartDate]","qryNegotiatedDates","[FdrID]=' " & [FdrID] & " ' ")));
  4.  
May 8 '07 #11
MMcCarthy
14,534 Recognized Expert Moderator MVP
I'm having trouble understanding the last part, do you mean to have quotes around the second [FdrID]? It's giving me a "Data type mismatch in critera expression" when I copy what you have and when I place quotes around [FdrID]. What does the single quotation do so that I may fully understand the logic.

Thank you.
The single quotes denote a text field rather than a number field. Are you sure the field is a text field?

BTW, I've been re-reading the thread and I'm just wondering where these values in the query are being drawn from. If these dates are only entered singly in their original table but are being repeated in the query you can't do what you are trying to do.

Mary
May 8 '07 #12
KOelschlaeger
7 New Member
Yes, you're deduction is correct. It has been entered singly in one table and being duplicated in the query. Do I have any other options?

Otherwise, now that I know I'm up against a wall, I'll attempt other possibilities.

Thank you.

The single quotes denote a text field rather than a number field. Are you sure the field is a text field?

BTW, I've been re-reading the thread and I'm just wondering where these values in the query are being drawn from. If these dates are only entered singly in their original table but are being repeated in the query you can't do what you are trying to do.

Mary
May 9 '07 #13
MMcCarthy
14,534 Recognized Expert Moderator MVP
Yes, you're deduction is correct. It has been entered singly in one table and being duplicated in the query. Do I have any other options?

Otherwise, now that I know I'm up against a wall, I'll attempt other possibilities.

Thank you.
If you are using this query for a report just group on the date and it will only show once.
May 9 '07 #14

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

Similar topics

14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
0
by: Mark Reed | last post by:
Hi all, The background is: I have a database at work which runs on 97. I have copied this database and bought it home to try new things with it. My home Access version is 2000. At home, I have...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Jan Szymczuk | last post by:
Thanks again guys, for your recent assistance. Now as you were so kind to assist the first time (the solution was so simple and elegant) I was wondering if you could assist with another couple of...
14
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I...
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
4
Zwoker
by: Zwoker | last post by:
Hi all, I'm not sure whether this should be posted in the MS Access forum or one of the SQL forums (which one?), so I'll start here. I'm self taught in the syntax of SQL based queries that I...
1
NeoPa
by: NeoPa | last post by:
Access QueryDefs Mis-save Subquery SQL Access stores its SQL for Subqueries in a strange manner :s It seems to replace the parentheses "()"with square brackets "" and (often) add an extraneous...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.