Results 1 to 3 of 3
  1. #1
    Daryl2106 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    167

    Selecting records with matching criteria

    Hi,

    I have a table of CourseNumbers that contains a four digit course number assigned to a particluar class each year. (The course number does not carry over from year to year). Each course number and its PK is associated with three FKs one each for CourseName,CourseSection and InstructorName. Some of the CourseNumbers are numerically the same from year to year but the PK is always unique because the course may have a new section, different instructor, etc. There are about 200 CourseNumbers in a single table; about 30 per year associated with a year field (2012, 2013, 2014).

    When students return, these CourseNumbers need to be updated (in the StudentEnrollment table) to reflect the new CourseNumber assigned for the current year (students can continue in a course for two years). I need to update these numbers based on the CourseName and InstructorName fileds where there is a match. That is, find last year's CourseNumber and this year's CourseNumber where the CourseName and Instructor fields match.

    I need to first select the StudentEnrollment records which need to be updated (which I can do). I then to need to update FK in each record to match the PK in the CourseNumbersT that corresponds to the new CourseNumber for the current year.

    My difficulty is in creating the query to match last year's CourseNumbers with this year's CourseNumber based on the InstructorName and CourseName fields being equal.

    I know this shouldn't be too difficult, I just can't seem to figure it out. I'm not great with SQL and I use the query builder a lot.



    Thanks in advance and take care,

    Daryl

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,627
    Sounds like you need to do a self-join on the Courses table, with a little twist.

    Do a query that constructs a field to calculate the previous year for each record.

    SELECT CourseNumber, [yearfield], InstuctorName, CourseName, [yearfield]-1 As PrevYr FROM Courses;

    Now do another query that joins that query to the Courses table linking on the Year/PrevYr, InstuctorName, CourseName fields.
    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
    Daryl2106 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    167
    Thanks June. It worked!

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

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  2. Selecting records based on criteria from user input
    By shabbaranks in forum Programming
    Replies: 1
    Last Post: 01-04-2012, 09:06 AM
  3. Replies: 1
    Last Post: 10-24-2011, 08:01 AM
  4. Replies: 1
    Last Post: 08-11-2011, 11:33 AM
  5. Matching based on criteria from multiple tables
    By Jonpro03 in forum Database Design
    Replies: 13
    Last Post: 08-04-2011, 10:29 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