473,473 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL query to find covering staff - Syntax error

13 New Member
Hi Everyone,

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
  1. SELECT aa.[Position code], p.[Title], p.[Description], aa.[Team], aa.[Payroll number], s.[First name], s.[Surname], s.[Description]
  2. 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])
  3. WHERE IN
  4. (SELECT ab.[Position code]
  5. FROM tblAllocations ab
  6. WHERE aa.[Position code]=ab.[Position code]
  7. AND aa.[Team]=ab.[Team]
  8. AND aa.[Payroll number]!=ab.[Payroll number]
  9. AND ((aa.[Acting]=-1 AND ab.[Current position]=0)
  10. OR (aa.[Current position]=0 AND ab.[Acting]=-1)));
The main query simply isolates the fields I want displayed on the final query. The sub-query is attempting to find any duplicated Position codes belonging to the same Team with different Staff (i.e. the old record of who was originally in the position remains in the database and simply becomes marked as Inactive or not current). Further, it then tries to filter those results based upon either the first or the second identified Staff being Acting or Not Current.

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
May 30 '11 #1

✓ answered by NeoPa

The format of the WHERE clause is wrong James (See line #3). There needs to be some form of field reference for IN to work with. IE :

Expand|Select|Wrap|Line Numbers
  1. WHERE [X] IN(...)

3 1748
NeoPa
32,556 Recognized Expert Moderator MVP
The format of the WHERE clause is wrong James (See line #3). There needs to be some form of field reference for IN to work with. IE :

Expand|Select|Wrap|Line Numbers
  1. WHERE [X] IN(...)
May 30 '11 #2
James Grant
13 New Member
Hi NeoPa, thanks for your rapid reply!

I'm sure you've heard this many times over, but within 10 minutes of posting this topic I thought to myself "Oh you've got to be kidding...THAT'S IT!" Glaringly obvious haha.

After updating the syntax as you also concluded the query worked perfectly and I'm on to my next query/report.

For clarity here is a copy of the functional, corrected code:
Expand|Select|Wrap|Line Numbers
  1. SELECT aa.[Position code], p.Title, p.Description, aa.Team, aa.[Payroll number], s.[First name], s.Surname, s.Description, aa.Acting, aa.[Current position]
  2. FROM (tblAllocations AS aa INNER JOIN tblPositions AS p ON aa.[Position code]=p.[Position code]) INNER JOIN tblStaff AS s ON aa.[Payroll number]=s.[Payroll number]
  3. WHERE aa.[Position code] 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)));
  4.  
May 31 '11 #3
NeoPa
32,556 Recognized Expert Moderator MVP
Always a pleasure James :-)
May 31 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: sam | last post by:
When I run this SQL query: SELECT u.*, o.* FROM users u, orders o WHERE TO_DAYS(o.order_date) BETWEEN TO_DAYS('2003-09-20')-10 AND TO_DAYS('2003-09-20')+10
1
by: Steve | last post by:
I just spent waaaaaaaaaaaayy too much time trying to track down an error that was incorrectly reported just now, and I would like to see if someone can explain to me why it was reported that way. ...
5
by: Phil Powell | last post by:
select event_date, event_name, event_text, event_is_public, event_is_reserved, event_img_path, event_img_alt, event_member_id, event_is_email_notify from event where show_entry = '1' and...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
by: KevLow | last post by:
Hi, Hope some kind soul can help me out here.. I'm trying to programmatically modify the column headings of a crosstab query such that it can be dynamic based on user specified period (Month...
5
by: orangeDinosaur | last post by:
Here's a section of code: for x in occupants: if x not in uniqueUsers and not in staff: uniqueUsers.append(x) elif x in staff and not in uniqueStaff: uniqueStaff.append(x) When I try to...
7
by: John Øllgård Jensen | last post by:
Hi Using MS Asccess 2000: In a query I'm trying to create a new field with following expression: FilmDate: Left(,4) The field "FilmNo" is another text field in the query. This is...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
13
Topbidder
by: Topbidder | last post by:
I have this error on the code Parse error: syntax error, unexpected '"' in /home/topbidd/public_html/bid2/bid_classic.php on line 159 now i thought the error was this It seems that the...
3
by: Ann Madden | last post by:
Hello - I am super green to php and mysql. I have received the following error: Parse error: syntax error, unexpected $end in C:\website\chart.php on line 84... I have been through the code matching...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.