-
March 27th, 2007, 03:47 PM
#1
Access 2002 EXISTS / IN clause problems
Hello all! Help please!
I have the following setup in an Access 2002 database.
A table "Students":
Employee_ID
Last_Name
First_Name
etc.
The tables "Class_Names," "Class_Schedule," and "Class_Roster" that contain class information. There is a 1:Many relationship between Students:Class_Roster.
I joined the 3 class information tables together in a query called "qryClass_Roster."
I need to construct a query that returns all Students who have successfully completed 6 specific classes, where
IdClass_Name = 1,2,3,4,5, or 6
I have begun to construct the SQL statement but have run into problems:
Code:
SELECT Students.Employee_ID, Students.Last_Name, Students.First_Name
FROM Students
WHERE Students.Employee_ID IN
(select * FROM qryClass_Roster WHERE
qryClass_Roster.Employee_ID =
Students.Employee_ID AND
qryClass_Roster.Attended = "Yes" AND
qryClass_Roster.IdClass_Name = 1);
generates the following error:
"You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to return only one field."
If I use the EXISTS statement then the query does not filter the data correctly, because the main query returns all records when the subquery finds at least one record:
Code:
SELECT Students.Employee_ID, Students.Last_Name, Students.First_Name
FROM Students
WHERE (((Exists (select * FROM qryClass_Roster WHERE qryClass_Roster.Employee_ID = Students.Employee_ID
AND qryClass_Roster.Attended = "Yes"
AND qryClass_Roster.IdClass_Name = 1))<>False));
returns all 248 records, when it should actually return only 158.
Help? Am I approaching this issue correctly?
-
March 27th, 2007, 04:08 PM
#2
Re: Access 2002 EXISTS / IN clause problems
I actually got that working but the SQL looks like a monkey wrote it. Can somebody help me trim this down/make it more efficient?
Code:
SELECT Students.Employee_ID, Students.Last_Name, Students.First_Name
FROM Students
WHERE Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 1)
AND Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 2)
AND Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 3)
AND Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 4)
AND Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 5)
AND Students.Employee_ID IN
(SELECT DISTINCT qryClass_Roster.Employee_ID FROM qryClass_Roster
WHERE qryClass_Roster.Employee_ID = Students.Employee_ID and qryClass_Roster.Attended = "Yes"
and qryClass_Roster.IdClass_Name = 6);
-
March 28th, 2007, 06:41 AM
#3
Re: Access 2002 EXISTS / IN clause problems
SELECT Students.Employee_ID, Students.Last_Name, Students.First_Name
FROM Students
WHERE Students.Employee_ID IN
(select * FROM qryClass_Roster WHERE
qryClass_Roster.Employee_ID =
Students.Employee_ID AND
qryClass_Roster.Attended = "Yes" AND
qryClass_Roster.IdClass_Name = 1);
The above wouldn't have worked, because in your sub query you are selecting mutiple fields (SELECT *), you should only return one field, so that you Where Students.Employee_id could be equated to the result.
But I'm guessing that these tables are related to each other, and therefore you should have been able to run the query using Joins, instead of sub queries???
JP
Please remember to rate all postings.
-
April 3rd, 2007, 08:01 AM
#4
Re: Access 2002 EXISTS / IN clause problems
Eeeek..
SELECT * FROM a WHERE a.col IN (SELECT b.col FROM b)
Should be avoided wherever possible.. Havent you people ever heard of JOINs?
SELECT * FROM a INNER JOIN b ON a.col = b.col
-
April 4th, 2007, 10:54 AM
#5
Re: Access 2002 EXISTS / IN clause problems
I did suggest that joins be used.
JP
Please remember to rate all postings.
-
April 4th, 2007, 10:58 AM
#6
Re: Access 2002 EXISTS / IN clause problems
and I did end up using Joins, and a nifty COUNT function that I had not considered previously.
Thanks anyway folks, always learn something from the replies here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|