Results 1 to 8 of 8
  1. #1
    ionbadea is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    2

    3rd Max for every student

    Hi,

    I have a simple table with students marks and I want to extract the 3rd highest mark for every one of them.
    Input table
    ID Name Mark
    1 Student1 5
    2 Student1 4
    3 Student1 3
    4 Student1 1
    5 Student2 8
    6 Student2 9
    7 Student2 10
    8 Student2 7



    The answer should be Student1=3 Student2=8. Could you tell me how to do it in MS Access 2013?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe you could use a SELECT TOP statement.
    http://www.w3schools.com/sql/sql_top.asp

  3. #3
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Access doesn't do rankings well in queries. It can be done as outlined here though:

    https://support.microsoft.com/en-us/kb/120608

    Cheers,

    Jeff

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    When you are looking for the 3rd highest, you can consider the following scenario:

    Get the highest number, where the number is not in the Top 2 highest numbers.

    That is ignore/remove the TOP 2, then the topmost(Top 1 remaining) is the 3rd highest of the original.

    Code:
    SELECT sname
          ,smark
    FROM studentmarks A
    WHERE a.smark IN (
            SELECT TOP 1 smark
            FROM studentmarks ZZ
            WHERE zz.sname = a.sname
                AND smark NOT IN (
                    SELECT TOP 2 smark
                    FROM studentmarks ww
                    WHERE ww.sname = a.sname
                    ORDER BY smark DESC
                    )
            )
    GROUP BY sname
        ,smark
    ORDER BY smark DESC;
    I used your data to create table StudentMarks with fields Id, SName and SMark.
    Result based on the data provided:

    sname smark
    Student2 8
    Student1 3

  5. #5
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    @orange, I like this!!

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Thanks InsuranceGuy. There may be other ways.
    Also, based on your previous post - I don't consider this a ranking query.
    It seems to me to be more - selecting specific records from groups.

  7. #7
    ionbadea is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    2
    Thanks Orange for your quick answer. When I writed your code into my DB I am asked to insert Sname and Smark. I presume I did not addapted something. I am an Access newbie. In your code syntax "FROM studentmarks A" what does "A" means?

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    A few things:

    Name is a reserved word in Access. see Reserved words
    I took your data and made a table.
    The table name is StudentMarks.
    The fields are ID, SMark and SName.
    "A" is an alias, as are ZZ and ww. see Using Aliases

    Good luck.

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

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2014, 08:47 AM
  2. Student Grades
    By BDevil15 in forum Access
    Replies: 4
    Last Post: 12-09-2013, 01:53 AM
  3. Student new to access
    By walter21907 in forum Access
    Replies: 6
    Last Post: 10-15-2013, 02:43 PM
  4. Student Database
    By jlc668 in forum Database Design
    Replies: 2
    Last Post: 10-09-2013, 03:26 PM
  5. Student Records
    By freekhenn in forum Access
    Replies: 1
    Last Post: 05-31-2013, 03:03 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