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

    Query Where Two Fields from a Table Are Linked

    I have a couple of queries that are getting too complicated for what I know how query so I'm hoping someone will be able to help me.
    This is my Table for entering Tournament Information
    Click image for larger version. 

Name:	Tournaments Table.jpg 
Views:	20 
Size:	80.4 KB 
ID:	13978

    What I'm wondering is if there is a way to link my winner column to the corresponding Team column they are in. I want to be able to query stuff like the rank or seed of a team when they are either a winner or loser where one time they might be in Team1 and the next time they might be in Team2. Should I create a fields for the Winner and Loser that say rank and seed?

    I also want to be able to do stuff like Filter by the a certain Rank, only show the times that team has one, and produce a count for the team that can be sorted.

    I have this table set up as a template so certain criteria already entered to save time so it would be nice not to have to drastically change the design of my table to be able to do such queries.

    If anybody has any input I would greatly appreciate it.

  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,770
    If you want to query for a team regardless of which field they are in, one way is with UNION query that normalizes the data. There is no wizard or designer for UNION, must type in the SQL view of query builder.

    SELECT Tournament, Round, Winner AS TeamName, "Winner" AS Result FROM tablename
    UNION SELECT Tournament, Round, Loser, "Loser" FROM tablename;

    Teams should have a name or ID that is unique across all regions. If each region has a TeamA and this is the value in stats table, queries will be more complicated, maybe impossible.
    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
    That's why you don't put that all in one table. Break it down to component facts. I don't know how often your ranks or seeds will be recalculated, so I can't tell you exactly how to break down your tables.

    I'll assume that within any tournament, the Ranks and Seeds will be set initially and stay the same.


    tblTeam
    teamID
    TeamName

    tblTeamRank
    Round
    Region
    TeamID
    Rank
    Seed

    In Round 1, East Region, Team A is Rank 1, Seed 1,
    In Round 1, East Region, Team B is Rank 64, Seed 16

    tblGames
    Gamenumber
    Round
    Region

    In Round 1, East Region, there is a Game X1,
    In Round 1, East Region, there is a Game X2,
    In Round 2, East Region, there is a Game Y1,



    tblPlays
    GameNumber
    TeamID
    WinOrLoss

    Team A Plays Game X1, Wins
    Team B Plays Game X1, Loses
    Team C Plays Game X2, Loses
    Team D Plays Game X2, Wins
    Team A Plays Game Y1, Loses
    Team D Plays Game Y1, Wins


    You can join these tables to produce your same "View" above, but you can get much more useful information out of them, rather than duplicating your seed and rank information in a highly redundant fashion.

  4. #4
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    I have things somewhat working. Using 5 queries to get the result seems like overkill but if it works I'm ok with it. I'm using parameters in certain fields I want to filter by and then using ([Tbl_Tournaments].[Winner]) in the Team criteria. I have one for team1 and one for team2. Then I have two queries off these where there is just a Team field and a winner field that has a count. Now in the last query I am trying to add the count of both queries. The issue I'm having is if a Team is only in one query they don't show up in the final query, only teams that are both in team1 and team2. Is there a way code in sql if one of the names doesn't exist in a query to just use 0 to add with?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    No, VBA won't help you with that, unless you want to write records to another table.

    What you need is a master dataset that has all the teams. Join the two queries to that master. If you don't have a master table of teams, the master can be created by a query, again use a UNION.
    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.

  6. #6
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Ok, I do have a master dataset that has all the teams. When I created this query I have team from that table set with a relationship to the team from each of the other queries. That's where I don't understand if they are linked why they all don't show up in the query.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What is the join type? Should be like "Show all records from Teams table ..."
    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.

  8. #8
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    I ended up getting this to work and without the multiple queries. I had my team name linked to team1 and when I switched it be linked to winner everything worked exactly as I wanted it to.

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

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2013, 05:59 PM
  2. Replies: 1
    Last Post: 02-18-2013, 02:55 AM
  3. Replies: 0
    Last Post: 01-17-2013, 07:43 AM
  4. Replies: 2
    Last Post: 08-22-2010, 07:59 PM
  5. Replies: 2
    Last Post: 10-27-2009, 07:09 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