Results 1 to 5 of 5
  1. #1
    sotssax is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    20

    Finding same ID that has both values in another field


    Hello all,

    I am trying to create a query that will find records with the same ID but with different values in another field.

    I have a table with the following fields.
    StudentID
    ViolationDate
    Offense

    What I would like to do is find students that have only offense "A" and exclude the students that have only offense "B" or have both "A" and "B".

    Any help will be greatly appreciated.

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Here's one way that works. It might not be the most sophisticated way to do this - but it works.
    I believe it may be possible to do this using a 'Not Exists' in the Where Clause - using a dupe table with an alias name . . .

    1. Create a query named 'QryStudents_With_B_Offense' that returns all Students who have a B Offense:
    Code:
    SELECT DISTINCT Offense.StudentID
    FROM Offense
    GROUP BY Offense.StudentID, Offense.ViolationDate, Offense.Offense
    HAVING (((Offense.Offense)="B"));
    2. Create a second query like this:
    Code:
    SELECT DISTINCT Offense.StudentID
    FROM Offense LEFT JOIN QryStudents_With_B_Offense ON Offense.[StudentID] = QryStudents_With_B_Offense.[StudentID]
    WHERE (((QryStudents_With_B_Offense.StudentID) Is Null));
    When you run the second query - you should get all the students who have ONLY an A offense against their names.
    I used 'Distinct' in both queries so that you only get each student name once.

    Let me know if this helps.

  3. #3
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Sorry . . . double-posted.
    Last edited by Robeen; 03-29-2012 at 02:07 PM. Reason: double post.

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    P.S. Once I said it could be done in a single query - using 'Not Exists - I HAD to try and make that work too.
    Here it is all in one query [using a sub-query]:
    Code:
    SELECT Distinct Offense.StudentID
    FROM Offense
    Where Not Exists
    (Select Distinct Dupe.StudentID
    From Offense as Dupe
    Where Dupe.StudentID = Offense.StudentID
    and Dupe.Offense = "B");
    Hope this helps!

  5. #5
    sotssax is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    20
    Robeen,

    The second way worked. I didn't try the first one. Thanks for your help!!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  2. Replies: 2
    Last Post: 06-11-2011, 10:39 PM
  3. Replies: 4
    Last Post: 10-15-2010, 07:50 AM
  4. Replies: 5
    Last Post: 08-07-2009, 05:23 PM
  5. Finding highest value in a text field
    By cdominguez in forum Queries
    Replies: 3
    Last Post: 06-02-2009, 09:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums