Results 1 to 8 of 8
  1. #1
    SashaSandy is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    12

    Query to calculate Grade using Lookup and innerjoins.


    I have a database with:
    2 tables, tblStudentMarks and tblGrades
    2 Queries qryResults and qryStudents_Grades

    Click image for larger version. 

Name:	final.jpg 
Views:	18 
Size:	185.8 KB 
ID:	13687


    1.qryResults:Calculates the TotalAverageMaths+English+Science)/3.
    2.qryStudents_Grades:Calculates the students grades based on the TotalAverage by comparing the total average to the min & max marks on the tblGrades.


    Grades are compared within a range specified in tblGrades with the following SQL wich is the query qryStudents_Grades:


    Code:
    SELECT SM.Student_ID, SM.Students, SM.TotalAverage, SM.Maths, SM.English, SM.Science, GD.Grade
    FROM qryResults AS SM INNER JOIN tblGrades AS GD ON (SM.TotalAverage>=GD.MinMarks) AND (SM.TotalAverage<=GD.MaxMarks);

    Question: i would like to also calculate the grades of the individual subjects in the same query (qryStudents_Grades), where the marks got by a student in the subject..say maths is compared to the (tblGrades) and grade is displayed in a colum say maths_grades for each student.
    How can i do this for all subjects in one query. (qryStudents_Grades)?
    Attached Files Attached Files

  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,626
    You can use DLookup.
    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
    SashaSandy is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    12
    this worked for me, Query SQL statement. however, I would like the one i posted in the question to work as well.
    Code:
    SELECT SM.Student_ID, SM.Students, SM.TotalAverage, SM.Maths, SM.English, SM.Science, (  SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= SM.TotalAverage
          ORDER BY GD.MinMarks DESC  
        )
    ) AS Grade,
    
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= SM.Maths
          ORDER BY GD.MinMarks DESC  
        )
    ) AS MathsGrade,
    
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= SM.English
          ORDER BY GD.MinMarks DESC  
        )
    ) AS EnglishGrade,
    
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= SM.Science
          ORDER BY GD.MinMarks DESC  
        )
    ) AS ScienceGrade
    FROM qryResults AS SM;

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    You mean like:
    Code:
    SELECT DISTINCT *
    FROM (SELECT Student_ID, Students, TotalAverage, Maths, English, Science, (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= TotalAverage
          ORDER BY GD.MinMarks DESC  
        )
    ) AS Grade,
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= Maths
          ORDER BY GD.MinMarks DESC  
        )
    ) AS MathsGrade,
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= English
          ORDER BY GD.MinMarks DESC  
        )
    ) AS EnglishGrade,
    
      (SELECT GD.Grade
      FROM tblGrades AS GD
      WHERE GD.MinMarks =
        (
          SELECT TOP 1 GD.MinMarks
          FROM tblGrades AS GD
          WHERE GD.MinMarks <= Science
          ORDER BY GD.MinMarks DESC  
        )
    ) AS ScienceGrade
    FROM qryResults) AS SM INNER JOIN tblGrades ON (SM.TotalAverage>=tblGrades.MinMarks) AND (SM.TotalAverage<=tblGrades.MaxMarks);
    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.

  5. #5
    SashaSandy is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    12
    Hallo! june7, Thanks for your reply, YES! your solution does part of the trick as mine...i will be more elaborate now.

    the database basically has to calculate the individual subjects grades and Equivalent GPA marks for the subjects then a Total Average for the Grades and GPA + show comments.
    the tblGrades has the fields: Grade(A,A-,B..etc),MinMarks,MaxMarks,MinGPA,MaxGPA and comment(good,Excellent...etc).
    the tblStudentMarks is where the subject marks are inputted
    the
    qryResults is where the Totalmarks is calculated (to be used to get the Average grade and Average GPA)
    the qryStudents_Grades is where (My problem comes in). i need THIS query to get the Grade,GPA and comment for individual subjects and then for the Average.

    am sorry i have posted pretty-much way outside what i wanted but i hope you get the clear picture now, i have tried numerous SQL statement for that particular query but i keep getting half results. i would really appreciate your help.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    I thought the query I provided does show the letter grade for the average? See the field named SM.Grade.

    What 'comment'?

    What is your formula for GPA? Don't you need points assignment (A=4, B=3, C=2, D=1, F=0) and course credit hours?
    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
    SashaSandy is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    12

    Lightbulb

    June7 Yes it did, thanks but this is what i wanted.
    SELECT SM.Student_ID, SM.Students, SM.class, SM.Maths, SM.English, SM.Science, (SELECT GD.Grade FROM tblGrades AS GD
    WHERE GD.MinMarks =
    (
    SELECT TOP 1 GD.MinMarks
    FROM tblGrades AS GD
    WHERE GD.MinMarks <= SM.TotalAverage
    ORDER BY GD.MinMarks DESC
    )
    ) AS Grade, (SELECT GD.Grade
    FROM tblGrades AS GD
    WHERE GD.MinMarks =
    (
    SELECT TOP 1 GD.MinMarks
    FROM tblGrades AS GD
    WHERE GD.MinMarks <= SM.Maths
    ORDER BY GD.MinMarks DESC
    )
    ) AS MathsGrade, (SELECT GD.Grade
    FROM tblGrades AS GD
    WHERE GD.MinMarks =
    (
    SELECT TOP 1 GD.MinMarks
    FROM tblGrades AS GD
    WHERE GD.MinMarks <= SM.English
    ORDER BY GD.MinMarks DESC
    )
    ) AS EnglishGrade, (SELECT GD.Grade
    FROM tblGrades AS GD
    WHERE GD.MinMarks =
    (
    SELECT TOP 1 GD.MinMarks
    FROM tblGrades AS GD
    WHERE GD.MinMarks <= SM.Science
    ORDER BY GD.MinMarks DESC
    )
    ) AS ScienceGrade, SM.TotalAverage, (SELECT GD.GPA FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.Maths ORDER BY GD.MinMarks DESC)) AS MathsGPA, (SELECT GD.GPA FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.English ORDER BY GD.MinMarks DESC)) AS EnglishGPA, (SELECT GD.GPA FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.Science ORDER BY GD.MinMarks DESC)) AS ScienceGPA, (([MathsGPA]+[EnglishGPA]+[ScienceGPA])/3) AS TotalGPA, (SELECT GD.Comment FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.Maths ORDER BY GD.MinMarks DESC)) AS MathsComment, (SELECT GD.Comment FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.English ORDER BY GD.MinMarks DESC)) AS EnglishComment, (SELECT GD.Comment FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.Science ORDER BY GD.MinMarks DESC)) AS Sciencecomment, (SELECT GD.Comment FROM tblGrades AS GD WHERE GD.MinMarks =(SELECT TOP 1 GD.MinMarks FROM tblGrades AS GD WHERE GD.MinMarks <= SM.TotalAverage ORDER BY GD.MinMarks DESC)) AS Totalmarkscomment, ([Maths]+[English]+[Science]) AS Totals
    FROM qryResults AS SM;


  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Issue solved?
    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.

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

Similar Threads

  1. Query to calculate
    By sha1023012 in forum Queries
    Replies: 4
    Last Post: 10-14-2012, 12:24 PM
  2. Replies: 4
    Last Post: 09-18-2012, 05:07 AM
  3. Replies: 1
    Last Post: 04-25-2012, 12:36 PM
  4. Calculate Date in Query
    By raytackettsells in forum Programming
    Replies: 8
    Last Post: 03-23-2012, 01:44 PM
  5. Calculate Grade
    By DCV0204 in forum Forms
    Replies: 4
    Last Post: 12-13-2011, 02:59 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