Results 1 to 10 of 10
  1. #1
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57

    Sum & Previous in a query


    In my database I have a table for tracking points for teams. I want to then create a query to provide a total which adds all points for the team together. Based on the sum I want to rank the teams. Should I put the rank in the query? If so, is there an easy way to automatically number them?

    Second question would be is there a way to show a total not counting the most current tournament or the previous rank to break ties. Attached is my current table and query.
    Click image for larger version. 

Name:	Points-Table.png 
Views:	12 
Size:	117.8 KB 
ID:	13967Click image for larger version. 

Name:	Points Qry.jpg 
Views:	12 
Size:	83.0 KB 
ID:	13966Click image for larger version. 

Name:	Points-Design View.jpg 
Views:	12 
Size:	103.1 KB 
ID:	13965

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    A GROUP BY query with aggregate calcs sorted by point total should be fairly straightforward. You appear to have accomplished that.

    Assigning a rank value is not so easy. Review http://allenbrowne.com/ranking.html
    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
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Assuming that your Tournament number field always increases, and that it is not possible for two different teams to get the same number of points in a tournament (like, both teams didn't show up?) then you could use something like this:
    Code:
    tblFirst
    Team     text     The Team Name
    TourNo   Number   The Tournament Number
    TourPts  Number   The Tournament Points Earned
    
    Query1:  
    SELECT
       Q1.Team,
       SUM(Q1.TourPts) AS CurrPts,
       SUM(Q1.PreVPts) AS PrevPts
    FROM 
       (SELECT
           T1.Team,
           T1.TourPts,
           IIF(T1.TourNo < (SELECT Max(T2.TourNo) 
                            FROM tblFirst AS T2
                            WHERE T1.Team = T2.Team),
               T1.TourPts,
               0)  AS PrevPts
        FROM 
           tblFirst AS T1) AS Q1
    GROUP BY Q1.Team; 
     
    Query2:
    SELECT 
       Q1A.Team,
       Q1A.CurrPts, 
       Count(Q1B.Team) AS RANK
    FROM 
       Query1 AS Q1A, 
       Query1 AS Q1B
    WHERE
      ( (Q1A.CurrPts + (Q1A.PrevPts/1000)) <= (Q1B.CurrPts + (Q1B.PrevPts/1000)))
    GROUP BY 
       Q1A.Team, Q1A.CurrPts;
    I cheated slightly to simplify the last query. Adding 1/1000th of the previous points should accomplish the tie-breaker without having to separately test for the equal condition.
    Last edited by Dal Jeanis; 10-03-2013 at 09:19 AM. Reason: fix IFF to IIF, add GROUP BY

  4. #4
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    In Query1 I am getting an error for Undefined function 'IFF' in expression

  5. #5
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Never mind, figured out that the IFF needed to be IIF

  6. #6
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Sorry, that' a holdover from my calculus days. I type IFF about half the time.

  7. #7
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Quote Originally Posted by Dal Jeanis View Post
    Assuming that your Tournament number field always increases, and that it is not possible for two different teams to get the same number of points in a tournament (like, both teams didn't show up?) then you could use something like this:
    Code:
    tblFirst
    Team     text     The Team Name
    TourNo   Number   The Tournament Number
    TourPts  Number   The Tournament Points Earned
    
    Query1:  
    SELECT
       Q1.Team,
       SUM(Q1.TourPts) AS CurrPts,
       SUM(Q1.PreVPts) AS PrevPts
    FROM 
       (SELECT
           T1.Team,
           T1.TourPts,
           IIF(T1.TourNo < (SELECT Max(T2.TourNo) 
                            FROM tblFirst AS T2
                            WHERE T1.Team = T2.Team),
               T1.TourPts,
               0)  AS PrevPts
        FROM 
           tblFirst AS T1) AS Q1
    GROUP BY Q1.Team; 
     
    Query2:
    SELECT 
       Q1A.Team,
       Q1A.CurrPts, 
       Count(Q1B.Team) AS RANK
    FROM 
       Query1 AS Q1A, 
       Query1 AS Q1B
    WHERE
      ( (Q1A.CurrPts + (Q1A.PrevPts/1000)) <= (Q1B.CurrPts + (Q1B.PrevPts/1000)));
    I cheated slightly to simplify the last query. Adding 1/1000th of the previous points should accomplish the tie-breaker without having to separately test for the equal condition.
    I managed to get the first query to work but the second query doesn't seem to want to work. When I try to run the query I get the following error:
    You tried to execute a query that does not include the specified expression 'Team' as part of an aggegate function

  8. #8
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Sorry - Add to the end
    Code:
    GROUP BY 
       Q1A.Team, Q1A.CurrPts;
    That's the problem with aircode - it takes a few shots to compile...

  9. #9
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Yeah, that's understandable. Thanks a bunch.
    Is there a way to push this information into a table or something so I can query out who's been ranked by number the most times or similar stuff with the ranking?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Can try UPDATE or MAKE TABLE sql actions.
    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 Help - calculate variance previous year
    By brtucker in forum Queries
    Replies: 1
    Last Post: 01-31-2013, 05:40 PM
  2. Replies: 2
    Last Post: 01-28-2013, 11:32 AM
  3. Query to use previous row's data ?
    By amrut in forum Queries
    Replies: 1
    Last Post: 07-25-2012, 06:20 AM
  4. Replies: 2
    Last Post: 06-05-2012, 08:31 PM
  5. Query that displays previous row as well
    By mccarthy in forum Queries
    Replies: 5
    Last Post: 02-20-2011, 05:31 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