After some head scratching I discovered that my code above is NOT working. So here I am to ask the question again.
In essence here is what I am looking for - I have a log which records events for users. It records Name, Date, Item, and Activity. What I want to do it identify all records where a specific Activity is logged. Then I want to return that record and every record which matches the Name, Item, and is within six hours prior to the date/time of the record that matched the Activity criteria.
I think this can be accomplished with a self-join but I am not adept at those so I created a query to identify the records with the specific Activity (QryFirstQuery) and I am trying to relate that query back to my original table based on the criteria. It it not working. It is getting close but I am getting repeated records.
The table (tblMyTable) has the fields: Name, Item, TheDate, Activity
The first query is:
Code:
SELECT tblMyTable.Name, tblMyTable.Item, tblMyTable.TheDate, tblMyTable.Activity
FROM tblMyTable
WHERE (((tblMyTable.Activity)="The Other"));
The last piece, where I hope to get the relevant records is:
Code:
SELECT tblMyTable.Name, tblMyTable.Item, tblMyTable.TheDate, tblMyTable.Activity
FROM QryFirstQuery INNER JOIN tblMyTable ON (QryFirstQuery.Item = tblMyTable.Item) AND (QryFirstQuery.Name = tblMyTable.Name)
WHERE (((tblMyTable.TheDate) Between [qryFirstQuery].[TheDate] And DateAdd("h",-1,[tblMyTable].[TheDate])));
Here is my data, I will describe what the colors are (I did this manually so I might have done it wrong):
Yellow means that the record got picked up by the first query.
Orange means that the record got picked up because it matches Name and Item and is six hours prior to a yellow cell.
id 1 gets picked up bc it is The Other. Nothing prior to it gets picked up bc there is nothing matching Name and Activity.
id 1 also gets picked up bc it is withing six hours prior to id 5 and matches names and activity
id 3 because matched id 5 Name, Activity and is within six hours
id 5 because The Other
id 11 because The Other
id 3 because matched id 14 Name, Activity and is within six hours
...