tags:

views:

51

answers:

4

I have another SQL/access 2007 question that seems really basic but I'm not sure how to Google for it.

I have this table STUDENT-TERMS with the following fields:

StudentTermID

StudentID (links to STUDENT table)

TermID (links to TERMS table)

TermGPA (this is the nugget of info that needs to be recorded)

STUDENT table looks like this:

StudentID

Name

TERMS table looks like this:

TermID

Start date

End date

I have a document with all of the info that gets uploaded into the Access doc. This document that I get has the same student and term IDs as exist in the database.

Then I can run an append query to get the data into the STUDENT-TERMS table. Which is fine.

How do I write the query such that there is only one record of each term for each student? So for example I have this data in the table right now:

StudentTerm ID: 5

StudentID: Tara

TermID: 1011Autumn

TermGPA: 3.8

When I upload and append a new document, it might still contain that information about Tara and I want the db to say "Hey, we already have that, skip it" as it does the appending. How do I indicate that?

HERE IS THE CONCLUSION/ANSWER/RESOLUTION: Thanks for your help everyone. This is what I wound up doing:

  1. I made a new field in the table called StudentTerm which contains a concatenation of the StudentID and the Term (like so: Tara1011Autumn). This field is indexed with no duplicates.

  2. The query that performs the appending looks like this:

INSERT INTO StudentTerms ( StudentID, TermID, GPA, StudentTerm ) SELECT Upload_Students.StudentID, Upload_Students.TermID, Upload_Students.GPA, [Upload_Students]![StudentID] & [Upload_Students]![TermID] AS Expr1 FROM Students INNER JOIN Upload_Students ON Students.StudentID = Upload_Students.StudentID;

This query attempts to fill the StudentTerm field with Tara1011Autumn. If that string already exists in the table, it won't append the record.

+1  A: 

Assuming you are doing a SQL based append query, you can use a NOT EXISTS clause.

Note
I'm assuming here that you can have multiple STUDENT-TERMS records for the same student if the StudentTermID is different.

INSERT INTO STUDENT-TERMS
          ( StudentTermId
          , StudentId
          , TermID
          , TermGPA )
     SELECT 5
          , 'Tara'
          , '1011Autumn'
          , 3.8
      WHERE NOT EXISTS
            (SELECT * FROM STUDENT-TERMS WHERE StudentID = 'Tara'
                                           AND StudentTermId = 5)
dcp
That will nearly work in Access
Remou
A: 

*Assumption database is in the 3NF *

SELECT st.StudentId, s.StudentName, t.TermId, t.TermName, st.TermGPA
FROM STUDENTS_TERMS st
INNER JOIN STUDENT s ON st.StudentID = s.StudentId
INNER JOIN TERMS t on st.TERMS = t.TermId
WHERE st.StudentID = 5
Vash
A: 

Based on dcp's answer but adjusted for Access

INSERT INTO STUDENT-TERMS
      ( StudentTermId
      , StudentId
      , TermID
      , TermGPA )
 SELECT DISTINCT 5
      , 'Tara'
      , '1011Autumn'
      , 3.8
  FROM STUDENT-TERMS
  WHERE NOT EXISTS
        (SELECT * FROM STUDENT-TERMS WHERE StudentID = 'Tara'
                                       AND StudentTermId = 5)
Remou
Wouldn't this insert zero rows if STUDENT-TERMS is empty?
Justin K
How would this look if I didn't know which records might duplicate, I just want to prevent any possible duplicates? In other words, how would you write this replacing the specific "Tara" and "5" with variables?
Tara
@Justin K Yes. However, a from table is needed in Access, but it can be any table in this case, even MsysObjects, a system table.
Remou
If you want to be sure of unique values, it is generally best to use a unique index.
Remou
Where did you see the variables coming from? Did you want parameters?
Remou
I don't know, just some way to not have to specify in the code which values will be duplicated. I won't know in advance which values should be excluded.
Tara
You are running the SQL in VBA? If so, you can use a parameter query or use variables with an SQL string, though you may have to watch for injection attack, depending on your environment.
Remou
+1  A: 

Create a unique index on the [student-terms] table, with the StudentID and StudentTermID fields in the index.

Then, when you run the append query Access will tell you "# records couldn't be added due to key violations", before asking you if you want to continue. If you hit Yes in that dialog box, then all the non-violating records will be appended, and the offending records will be saved into a different table.

There are other options available in the DoCmd.RunQuery action if you're doing this from VBA to automate this.

corey lawson
If the business requirement is that the a studentTerm record is unique, then use a unique index to enforce it.
Adam Musch