user-hostile
Registered User.
- Local time
- Today, 16:19
- Joined
- Oct 20, 2007
- Messages
- 10
Hello. I thought I could handle this one, but I'm just not getting anywhere. My situation is pretty simple: I have two tables, with a one-to-many relationship. Both tables have primary keys, and the primary and foreign keys are set up correctly. Referential integrity is enforced. It's a pretty simple join/relationship. OK...
If I build a query and select a row from my "one" side table, obviously 0, 1, or more than 1 related records can be selected in the "many" side table. Very ordinary, and most of the time, that's what I need.
But now, instead of returning the "many" related records from the "many" side table, I want to select only ONE record, based on the most recent date value in one of the date fields. That is, if I build a query that selects a particular record on the "one" side, I want ONLY the most recent (by virtue of the date field) record from the "many" side table to return. I don't want the other records that are related. AND, I need to be able to return (in the same record set) the "many" side table's unique ID.
I'm writing on the fly SQL below (don't have Access on this machine), so it may not be exact, but here's an idea what I'm trying for:
SELECT Max(ManyTable.MyDate) as MaxDate, ManyTable.ManyTableID, OneTable.OneTableID FROM ManyTable INNER JOIN OneTable ON OneTable.OneTableID = ManyTable.OneTableID
This SQL returns all the related records from the "many" table, even though I thought my Max() function would only return the latest date value.
SUMMARY: I just want a "temporary" one-to-one recordset in my query, with the one (on the "many" side) simply the record that has the greatest date value in my specified date field. I've tried a dozen different totals queries, crosstabs, etc. I can't believe I'm having such a block about this. Any help is appreciated; thanks.
U-H
If I build a query and select a row from my "one" side table, obviously 0, 1, or more than 1 related records can be selected in the "many" side table. Very ordinary, and most of the time, that's what I need.
But now, instead of returning the "many" related records from the "many" side table, I want to select only ONE record, based on the most recent date value in one of the date fields. That is, if I build a query that selects a particular record on the "one" side, I want ONLY the most recent (by virtue of the date field) record from the "many" side table to return. I don't want the other records that are related. AND, I need to be able to return (in the same record set) the "many" side table's unique ID.
I'm writing on the fly SQL below (don't have Access on this machine), so it may not be exact, but here's an idea what I'm trying for:
SELECT Max(ManyTable.MyDate) as MaxDate, ManyTable.ManyTableID, OneTable.OneTableID FROM ManyTable INNER JOIN OneTable ON OneTable.OneTableID = ManyTable.OneTableID
This SQL returns all the related records from the "many" table, even though I thought my Max() function would only return the latest date value.
SUMMARY: I just want a "temporary" one-to-one recordset in my query, with the one (on the "many" side) simply the record that has the greatest date value in my specified date field. I've tried a dozen different totals queries, crosstabs, etc. I can't believe I'm having such a block about this. Any help is appreciated; thanks.
U-H