My setup is I have two tables, Study and Activity_History. Activities run on studies so there is a 1:many relationship.
I want to be able to run a SQL query on an Activity_History table which will get me the activity and the previously run activity. I currently have this:
SELECT
*
FROM Activity_History AS A1
LEFT JOIN Activity_History AS A2
ON A2.Parent_Study_ID =
(
SELECT TOP 1 Parent_Study_ID
FROM Activity_History AS A3
WHERE A3.Parent_Study_ID = A1.Parent_Study_ID
AND A3.Activity_Date < A1.Activity_Date
ORDER BY Activity_Date DESC
)
This is not working. What's happening is its pulling the Activity_Date party of the query has no effect and it just returns the first matching Activity_Date in descending date order for every row. I think this is happening because in my subquery I am using Activity_Date in the where, but this is not in the subquery select.
Thanks for any help!
WHEREin yourSELECT... what's the error that you're getting?