To my understanding this code should work; however there's a syntax error somewhere within the sub-query and I cannot find it. It's very possible that my understanding is insufficient and there's a better way to achieve my ends, hence a little background on the motivation behind the query:
I run a staffing database for my department. My boss wants a report that can be run to identify which staff members are covering or 'acting up' and whom they're replacing. Using 3 of my tables (Allocations, Positions, Staff; which allows a Staff member to be Allocated a Position) I can already identify which staff are acting up, and which staff are not current. Logically this then allows the user (if sufficiently familiar with the department's staff) to figure out who's filling in.
This is not what my boss wants - because the report will be passed further up the chain (to people who are not familiar with the department's staff) they need a report that identifies both the replacement and whom-was-replaced side-by-side.
So I'm trying to develop the following query:
Expand|Select|Wrap|Line Numbers
- SELECT aa.[Position code], p.[Title], p.[Description], aa.[Team], aa.[Payroll number], s.[First name], s.[Surname], s.[Description]
- FROM ((tblAllocations aa INNER JOIN tblPositions p ON aa.[Position code]=p.[Position code]) INNER JOIN tblStaff s ON aa.[Payroll number]=s.[Payroll number])
- WHERE IN
- (SELECT ab.[Position code]
- FROM tblAllocations ab
- WHERE aa.[Position code]=ab.[Position code]
- AND aa.[Team]=ab.[Team]
- AND aa.[Payroll number]!=ab.[Payroll number]
- AND ((aa.[Acting]=-1 AND ab.[Current position]=0)
- OR (aa.[Current position]=0 AND ab.[Acting]=-1)));
The bolded IN above shows where Access is finding a syntax error. I'm sure I'm either asking too much of a single query, have missed an obvious typo, or am attempting to do this the wrong way entirely. Originally I simply tried to do all of this utilising a single-level query (no sub-query) but I was unable to find the correct syntax for creating a duplicate table (i.e. Allocations AS aa, AS ab) whilst also implementing a JOIN.
Many thanks for any assistance, please let me know if you need any more information.
James