Results 1 to 14 of 14
  1. #1
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114

    Trying to Avoid Crosstab - Junction Table

    I have the following 4 tables:
    tblCourses
    tblPeople
    tblRoles
    tblCoursePeopleRoles

    tblCoursePeopleRoles is a junction table that accomodates the many-to-many relationships between the records in the first three tables.

    The junction table looks something like this:



    Math 25, John Smith, Instructor
    Math 25, Carol Jones, TA
    English 17, David Black, Instructor
    Spanish 1, Esperanza Martinez, Instructor
    Spanish 1, Esperanza Martinez, TA
    History 100, Jeff Michaels, Instructor
    History 100, Jeff Michaels, TA
    History 100, Susan Oats, Reader

    Given my junction table, what kind of query can I run to identify, for example, the following groups:

    People who are TAs and have no other roles (e.g., Carol Jones)
    People who are Readers and have no other roles (e.g., Susan Oats)
    People who are both an Instructor and a TA in the same course (e.g., Esperanza Martinez and Jeff Michaels)

    The problem: if I add all my tables to a select query and set "TA" as a criteria, it returns everyone who has TA designation, including those people who are both TA and Instructor in the same course. I need to see only those who are TAs but have no other role in the same course.

    How can I achieve that?
    Thank you.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    How many Roles are possible - only 3?

    Try:
    SELECT Course, Person, Max(IIf([Role]="Instructor",[Role],Null)) AS Instructors, Max(IIf([Role]="TA",[Role],Null)) AS TAs, Max(IIf([Role]="Reader",[Role],Null)) AS Readers
    FROM tblCoursePeopleRoles
    GROUP BY Course, Person;

    Add filter criteria as desired: Is Null / Not Is Null for the role fields.
    Last edited by June7; 01-08-2012 at 02:20 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    I will have to try this out tomorrow morning and see how it works.
    Is it normal to need so many queries to accomplish something seemingly simple?
    Is my database not designed or normalized properly?
    Thanks.

  4. #4
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Yes, 3 roles are possible.

  5. #5
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    What is the function of the MAX function here?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The IIf expression restricts the field values to two possibilities - the specified role or Null. The Max in each expression works with the GROUP BY to return single record for each course/person pair. The Max for each pair in each field will be the role, if one exists. Without it, the records will not consolidate. Don't just take my word, experiment, try without it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    I look forward to experimenting with it tomorrow. Thank you.
    Is this preferable to using a crosstab query?
    My junction table also contains salary information. Each Course, Person, Role combination has a different salary. So using a crosstab to consolidate, I can check which salary fields are null.
    But I have been told that crosstab should be a last resort, and I am not sure why...

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    It's because crosstabs are so dynamic. Depending on nature of the data and what is being pivoted, the columns can be different for each run. It is hard to design reports where the column names can change because the data may be filtered for a date range. Values in one period might not exist in another. If it is necessary to build reports on crosstabs, must be done very carefully or else have to rebuild every time because fields are no longer available for the bound textboxes and new fields need to be bound.

    If you don't care about building a report or the crosstab could be stable enough to be report RecordSource, then go ahead and create crosstab. You might like it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    I can't say I totally understand it, but it does work...
    I thought Max could only be used with numeric values, not text...
    Thank you!

  10. #10
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Quote Originally Posted by June7 View Post
    The IIf expression restricts the field values to two possibilities - the specified role or Null. The Max in each expression works with the GROUP BY to return single record for each course/person pair. The Max for each pair in each field will be the role, if one exists. Without it, the records will not consolidate. Don't just take my word, experiment, try without it.
    I am now using the query you suggested to get instructor name as well:

    Max(IIf([tblRoles.Role]="Instructor",[tblPeople.FirstName] & " " & [tblPeople.LastName],Null)) AS Instructor, Max(IIf([tblRoles.Role]="TA",[tblPeople.FirstName] & " " & [tblPeople.LastName],Null)) AS TA, Max(IIf([tblRoles.Role]="Reader",[tblPeople.FirstName] & " " & [tblPeople.LastName],Null)) AS Reader.

    I just realized that I sometimes have a course that is co-taught by two instructors. My junction table can accommodate that, but that defeats the purpose of using the Max function.

    Is there a way around this? I.e., if there are two instructors, list both of them under Instructors in the query?

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Your junction table shows names. Why does your Max expression refer to two tables? Are you actually saving a PeopleID but showed names in the data sample? You are doing these expressions in a query that joins tblPeople to tblCoursePeopleRoles?

    Since the GROUP BY is on both course and person, both instructors will list. For example, add a record to your sample data
    English 17 Susan Oats Instructor

    The query will result in two lines for Susan Oats, one for each class she has a role in. Getting both instructors on one line for the same course is not possible with this method. At the point VBA code will probably be required.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Quote Originally Posted by June7 View Post
    Your junction table shows names. Why does your Max expression refer to two tables?
    I am using the Max expression in two ways: to get the names from tblPeople and to get salary information from the junction table, tblCoursePeopleRoles.

    Quote Originally Posted by June7 View Post
    Are you actually saving a PeopleID but showed names in the data sample?
    Yes.

    Quote Originally Posted by June7 View Post
    You are doing these expressions in a query that joins tblPeople to tblCoursePeopleRoles?
    Yes.

    Quote Originally Posted by June7 View Post
    Getting both instructors on one line for the same course is not possible with this method. At the point VBA code will probably be required.
    I think this is what I need. Any suggestions?

    Sorry, as is clear, I am pretty new to this...

    Thanks again for your help.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Here is one way to concatenate records. Use the query built so far as the data source for the code.
    http://allenbrowne.com/func-concat.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Thank you. I will take a look.

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

Similar Threads

  1. How to use Junction Table to populate DB
    By Sorbz62 in forum Forms
    Replies: 1
    Last Post: 10-02-2011, 05:45 PM
  2. Populating Junction table from form
    By JFo in forum Access
    Replies: 18
    Last Post: 09-05-2011, 09:15 PM
  3. Enter Data into a Junction Table
    By darkwan75 in forum Database Design
    Replies: 3
    Last Post: 04-13-2011, 08:55 PM
  4. Updating a Junction Table
    By oleBucky in forum Forms
    Replies: 4
    Last Post: 04-05-2011, 04:59 PM
  5. Junction Table
    By mae3n in forum Database Design
    Replies: 2
    Last Post: 01-15-2011, 10:23 PM

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