CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2003
    Posts
    24

    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?

  2. #2
    Join Date
    Feb 2003
    Posts
    24

    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);

  3. #3
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    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.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: Access 2002 EXISTS / IN clause problems

    I did suggest that joins be used.
    JP

    Please remember to rate all postings.

  6. #6
    Join Date
    Feb 2003
    Posts
    24

    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
  •  





Click Here to Expand Forum to Full Width

Featured