Results 1 to 4 of 4

SELECT TOP query - Only returning 3 values when duplicates are allowed

  1. #1
    SALAlly is offline Novice
    Windows XP Access 2016
    Join Date
    Apr 2018
    Location
    Scotland
    Posts
    2

    Unhappy SELECT TOP query - Only returning 3 values when duplicates are allowed

    Hey,

    I've been developing my first MS Access database for a while now and making some steady progress. I've got various tables and queries set up which are all working well, but am stuck on one particular issue.

    This is an athletics scoring database, which converts an athletes individual performance into scores.

    I'm using SELECT TOP 3 to return the best three scores from a list of performances by any given athlete.
    Sometimes an athlete will score the same points for repeated performances - imagine your best 5 results score 700, 700, 650, 650 and 400 points. I therefore only want to total 700+700+650=2050points.

    My code is
    [code]

    SELECT t.AthleteID, t.AthleteName, t.Gender, t.AgeGroup, t.ClubID, t.ClubCategory, t.EventID, t.UniquePerfID, t.Score
    FROM [T05 All Throws Performances] AS t
    WHERE (((t.Score) In (SELECT TOP 3 Score
    FROM [T05 All Throws Performances]
    WHERE AthleteID = t.AthleteID
    ORDER BY Score DESC
    )))


    ORDER BY t.AthleteID, t.Score DESC;


    [\code]

    What this is doing is returning 700+700+650+650=2700, how do I drop the lowest duplicate score and only total 3?

  2. #2
    isladogs's Avatar
    isladogs is offline Very idle programmer
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    3,732
    Welcome both to MS Access Forums and Access World Forums where you have also posted this question
    https://www.access-programmers.co.uk...=1#post1572778

    For future info, there's nothing wrong with cross posting on more than one forum BUT you should always state you have cross posted and provide the link yourself

    As I've already explained at AWF, I think you will need to make the subquery into a ranking query.
    Suggest you read this thread where I gave several examples using a Serialize function
    https://www.access-programmers.co.uk...d.php?t=297922
    Colin (Mendip Data Systems) : Website, email
    If this has helped, please click the star button and leave a comment

  3. #3
    SALAlly is offline Novice
    Windows XP Access 2016
    Join Date
    Apr 2018
    Location
    Scotland
    Posts
    2
    Thanks for this and some good ideas of where to look and search.
    This thred - https://stackoverflow.com/questions/...plicate-values
    solved my issue.
    My code now looks like -
    SELECT t.AthleteID, t.AthleteName, t.Gender, t.AgeGroup, t.ClubID, t.ClubCategory, t.EventID, t.UniquePerfID, t.Score
    FROM [T05 All Throws Performances] AS t
    WHERE (((t.UniquePerfID) In (SELECT TOP 3 UniquePerfID
    FROM [T05 All Throws Performances]
    WHERE AthleteID = t.AthleteID
    ORDER BY Score DESC, UniquePerfID
    )))
    ORDER BY t.AthleteID, t.Score DESC, t.UniquePerfID;

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,584
    Post 3 was moderated, I'm posting to trigger email notifications.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. query returning duplicates
    By hak in forum Queries
    Replies: 17
    Last Post: 09-28-2017, 02:48 PM
  2. Set Allowed Values for A Textbox
    By Richard_Marx in forum Forms
    Replies: 3
    Last Post: 08-16-2016, 06:28 AM
  3. help with query returning duplicates
    By BrockWade in forum Queries
    Replies: 7
    Last Post: 12-05-2013, 02:23 PM
  4. Query Returning Duplicates
    By rlsublime in forum Queries
    Replies: 14
    Last Post: 03-25-2013, 11:26 AM
  5. Select Query returning no Results
    By Rhemo in forum Access
    Replies: 2
    Last Post: 09-15-2012, 04:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums