Results 1 to 7 of 7
  1. #1
    mvilela is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2024
    Posts
    4

    TOP function - Return only complete groups

    I have a query to group team athletes.


    SELECT Consulta_Jovens.ID, Consulta_Jovens.cla_escalao, Consulta_Jovens.cla_geral, Consulta_Jovens.voltas, Consulta_Jovens.chegada, Consulta_Jovens.tempo, Consulta_Jovens.t_liquido, Consulta_Jovens.dorsal, Consulta_Jovens.equipa, Consulta_Jovens.nome, Consulta_Jovens.cidade, Consulta_Jovens.escalao, Consulta_Jovens.cla, Consulta_Jovens.pontos
    FROM Consulta_Jovens
    WHERE (((Consulta_Jovens.dorsal) In (SELECT TOP 3 dorsal FROM Consulta_Jovens AS TT WHERE TT.equipa=Consulta_Jovens.equipa ORDER BY TT.pontos desc, TT.ID desc)));




    This query returns all participating teams.
    I intended to only return the complete teams (in this case 3 athletes).

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Are you saying a team is only 3 athletes?

    It should be returning the TOP 3 dorsal from each equipa because of the correlated subquery WHERE clause. If you want something different, please clarify.

    Could help to provide sample data - raw and expected output. Can build tables in post (Go Advanced edit toolbar or copy/paste Access records or Excel cells) or attach file (follow instructions at bottom of my post).
    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
    mvilela is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2024
    Posts
    4

    TOP funtion

    Quote Originally Posted by June7 View Post
    Are you saying a team is only 3 athletes?

    It should be returning the TOP 3 dorsal from each equipa because of the correlated subquery WHERE clause. If you want something different, please clarify.

    Could help to provide sample data - raw and expected output. Can build tables in post (Go Advanced edit toolbar or copy/paste Access records or Excel cells) or attach file (follow instructions at bottom of my post).
    All athletes belong to a team. The best 3 teams count towards the team ranking, provided that they have 3 athletes who have completed the race.
    The general table is imported directly from a CSV file.
    I am a beginner in Access.
    The database is attached.
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Quote Originally Posted by mvilela View Post
    The best 3 teams count towards the team ranking
    Did you mean the "The best 3 athletes"?

    Why are ID and cla_geral the same data? Why have both fields?

    There can be 1 to 18 athletes per team?

    If you want query to pull all VET records, could do escalao LIKE "VET*"

    Still not clear to me what the issue is. The query is returning top 3 athletes per team. If team has less than 3 athletes, it returns what they have. Do you want to not include those teams? That will complicate query as this will require in aggregate query that counts how many athletes in each team.
    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
    mvilela is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2024
    Posts
    4
    Quote Originally Posted by June7 View Post
    Did you mean the "The best 3 athletes"?

    Why are ID and cla_geral the same data? Why have both fields?

    There can be 1 to 18 athletes per team?

    If you want query to pull all VET records, could do escalao LIKE "VET*"

    Still not clear to me what the issue is. The query is returning top 3 athletes per team. If team has less than 3 athletes, it returns what they have. Do you want to not include those teams? That will complicate query as this will require in aggregate query that counts how many athletes in each team.
    There are 2 team classifications; "Jovens" and "Adultos".
    Adultos: "Elites" Or "VET_30" Or "VET_35" Or "VET_40" Or "VET_45" Or "VET_50" Or "VET_55" Or "VET_60" Or "Juniors" Or "feminine" - Consulta_Adultos_TOP3
    Jovens: "Benjamin_F" Or "Benjamin_M" Or "Infantil_F" Or "Infantil_M" Or "Iniciado_M" Or "Iniciado_F" Or "Juvenil_M" Or "Juvenil_F" Or "Cadete_F" Or "Cadete_M" - Consulta_jovens_TOP3


    First, the classification by echelon "cla_escalao" is drawn up and points are awarded to the athletes according to the "pontos" table


    Then, the classification by teams is drawn up according to the groupings indicated above, where only the teams count. that have at least 3 athletes. The points of the 3 best athletes of each team count.


    I want a query that sorts first by teams with 3 athletes, and then by team score.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    As I said, somewhere you need do an aggregation of data that counts how many athletes in each team then restrict records by that.

    SELECT equipa, Count(nome) AS CountAthlete FROM geral GROUP BY equipa;

    Then include that query in Consulta_Adultos joining on equipa fields, add filter criteria: CountAthlete >= 3

    Or use DCount() domain aggregate function, however, performance will likely be slower.
    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
    mvilela is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2024
    Posts
    4
    Quote Originally Posted by June7 View Post
    As I said, somewhere you need do an aggregation of data that counts how many athletes in each team then restrict records by that.

    SELECT equipa, Count(nome) AS CountAthlete FROM geral GROUP BY equipa;

    Then include that query in Consulta_Adultos joining on equipa fields, add filter criteria: CountAthlete >= 3

    Or use DCount() domain aggregate function, however, performance will likely be slower.


    Thank you for your help, which was excellent in resolving the issue.

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

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2019, 07:23 PM
  2. Top 4 multiple groups
    By nggman in forum Programming
    Replies: 4
    Last Post: 12-23-2018, 09:12 PM
  3. Replies: 6
    Last Post: 04-04-2017, 02:50 AM
  4. Replies: 3
    Last Post: 05-16-2013, 12:00 PM
  5. Replies: 2
    Last Post: 06-27-2012, 05:02 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