I've searched the site, and not found anything that really helps. I have a partial solution - I can return the records I want, but that then causes other problems.
To give you a bit of background, I'm maintaining a database that isn't my design, but I will soon be replacing it, so I don't want to spend too much time working on the existing one.
I have two tables, one for Customers, and one for Letters.
The only really relevant information in the customer table is the reference. The letters table contains a letter type, letter date, and customer reference.
I have a query that displays all records from the customer table, where a corresponding record exists in the letters table that's over a certain number of days old, and matches a certain letter type. This is used as the data source for a form, for users to update records (a specified number of days after a letter is sent, the account is ready to be looked at again).
The SQL is something like this. There's a lot of completely unrelated data in the customer table, so I've simplified my actual query. This is the only part I'm interested in.
The query works, but it's very possible to have more than one matching record in the letters table - the same letter can be sent multiple times. I want to be able to base my query on the most recent letter of a particular type.
Instead of joining the customer table to the letters table, I made a further query to join the accounts table to, that only showed the most recent record.
This gives me the most recent letter for each reference and type. If I call this query LettersGroup, then my original query would be:
This now displays exactly the records I want. All customer records where the most recent letter of a particular type was sent over a certain number of days ago.
My only problem now, is because I'm joining to a query that is grouped to limit the records returned, the original query is no longer updatable. Is there a way around this, or another way of only displaying the most recent letter record? I've played with a few things, but I'm much more familiar with VBA, and only really just getting to grips with SQL, so it's not been very productive.
Maybe there's a completely different approach that I'm not aware of. Any help would be greatly appreciated.
thanks
Ann
To give you a bit of background, I'm maintaining a database that isn't my design, but I will soon be replacing it, so I don't want to spend too much time working on the existing one.
I have two tables, one for Customers, and one for Letters.
The only really relevant information in the customer table is the reference. The letters table contains a letter type, letter date, and customer reference.
I have a query that displays all records from the customer table, where a corresponding record exists in the letters table that's over a certain number of days old, and matches a certain letter type. This is used as the data source for a form, for users to update records (a specified number of days after a letter is sent, the account is ready to be looked at again).
The SQL is something like this. There's a lot of completely unrelated data in the customer table, so I've simplified my actual query. This is the only part I'm interested in.
Code:
SELECT Customers.Reference, Letters.LetterType, Letters.LetterDate
FROM Customers LEFT JOIN Letters ON Customers.Reference = Letters.Reference
WHERE (((Letters.LetterType)="Letter1") AND ((Letters.LetterDate)<Date()-13));
The query works, but it's very possible to have more than one matching record in the letters table - the same letter can be sent multiple times. I want to be able to base my query on the most recent letter of a particular type.
Instead of joining the customer table to the letters table, I made a further query to join the accounts table to, that only showed the most recent record.
Code:
SELECT Letters.reference, Letters.LetterType, Max(Letters.LetterDate) AS MaxLetterDate
FROM Letters
GROUP BY Letters.reference, Letters.LetterType;
This gives me the most recent letter for each reference and type. If I call this query LettersGroup, then my original query would be:
Code:
SELECT Customers.Reference, LettersGroup.LetterType, LettersGroup.LetterDate
FROM Customers LEFT JOIN LettersGroup ON Customers.Reference = LettersGroup.Reference
WHERE (((LettersGroup.LetterType)="Letter1") AND ((LettersGroup.LetterDate)<Date()-13));
This now displays exactly the records I want. All customer records where the most recent letter of a particular type was sent over a certain number of days ago.
My only problem now, is because I'm joining to a query that is grouped to limit the records returned, the original query is no longer updatable. Is there a way around this, or another way of only displaying the most recent letter record? I've played with a few things, but I'm much more familiar with VBA, and only really just getting to grips with SQL, so it's not been very productive.
Maybe there's a completely different approach that I'm not aware of. Any help would be greatly appreciated.
thanks
Ann