Results 1 to 10 of 10
  1. #1
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66

    Code: Group and select first records

    I am trying to figure out how I would write a code that will recognize information by same “ID” and then only take the first 6 years of that information. Each row has an ID and a corresponding year with other information associated with it. The data looks similar to this:


    ID Year Avg HR
    JohnSmith 1988 .789 78
    JohnSmith 1989 .854 85
    JohnSmith 1990 .456 85
    TomJones 1978 .465 56
    TomJones 1979 .165 85

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    What is your table structure? What fields?

    Can you be more specific?

    write a code ---create a query There are free M$oft videos here

  3. #3
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    There are three tables imported from three different excel spreadsheets. I'm still working on which fields I actually will need. The problem I am having is figuring out the code to select the first "6" years of an ID. I think if I can be helped on that I will be able to add the rest of the fields I need easily enough... Well that is the plan at least.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442

  5. #5
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    Below is the code I was able to get from those links. From this code it gave me the top 6 years but not the top 6 years for each player. How do I get it to give me top 6 years for each player?

    SELECT Batting.playerID, Batting.yearID, Batting.G

    FROM Batting
    WHERE (((Batting.yearID) In (Select Top 6 [yearID] From [Batting] where [playerID]=[Batting].[playerID] Order by [yearID] Desc)))
    ORDER BY Batting.yearID DESC;

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Post a sample of your data

  7. #7
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    As in attach the database? I don't see how to attach it?

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you have to click the 'go advanced' button on the bottom right of the posting area just zip it up before posting and remove any private information.

  9. #9
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66

    Database

    Attached is the file. Thank you for your help.
    Attached Files Attached Files

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I followed the guide exactly on the last post of the link I gave, just substituting your field names.

    Create this query first:

    Code:
    SELECT Batting.playerID, Batting.yearID, IIf(IsNull([ab]) Or [ab]=0,0,[h]/[ab]) AS BatAvg
    FROM Batting
    ORDER BY Batting.playerID, IIf(IsNull([ab]) Or [ab]=0,0,[h]/[ab]) DESC;
    Save it as QryPre

    Create this query second:

    Code:
    SELECT PlayerID, YearID, BatAvg FROM QryPre WHERE BatAvg IN (SELECT TOP 6 BatAvg FROM QryPre as PreQry WHERE PreQry.PlayerID = QryPre.PlayerID ORDER BY BatAvg DESC)
    top 6 years (by batting average) for each player

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

Similar Threads

  1. SELECT & GROUP BY Clause
    By johnseito in forum Access
    Replies: 5
    Last Post: 07-11-2014, 10:55 PM
  2. Select into with group
    By Dennis Willis in forum Queries
    Replies: 1
    Last Post: 07-08-2014, 08:23 PM
  3. Replies: 17
    Last Post: 04-24-2014, 10:58 AM
  4. GROUP by yet SELECT fields not within GROUP BY
    By johnseito in forum Access
    Replies: 25
    Last Post: 11-03-2013, 10:20 PM
  5. Replies: 5
    Last Post: 07-29-2011, 11:54 AM

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