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

    Help with Combining Queries

    I have two queries, one that counts the times a team has beaten a seed in a tournament and one that counts the times a team has lost to a seed in tournament. One of the queries returns 18 results and one returns 21. What I am trying to do is combine the two queries so I can calculate a winning percentage to see who has been doing the best. How do i get the teams that aren't in both queries to show up with zeros in either the wins or losses column? Currently I am only getting the results of teams that have both a win and a loss.

    Here is my sql:
    SELECT Tbl_Teams.Team, [Copy Of Qry_Tournaments_Sort vs Winner Count].CountOfWinner, [Copy Of Qry_Tournaments_Sort vs Loser Count].CountOfLoser
    FROM (Tbl_Teams INNER JOIN [Copy Of Qry_Tournaments_Sort vs Winner Count] ON Tbl_Teams.Team = [Copy Of Qry_Tournaments_Sort vs Winner Count].Team) INNER JOIN [Copy Of Qry_Tournaments_Sort vs Loser Count] ON Tbl_Teams.Team = [Copy Of Qry_Tournaments_Sort vs Loser Count].Team;

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Do a LEFT OUTER join from this table to each of your other queries. This will make sure that no records from your main table are dropped in the event that there isn't a match.

    You can then use the NZ function to convert the Nulls from non-matched records to zeroes, i.e.
    NZ( [Copy Of Qry_Tournaments_Sort vs Winner Count].CountOfWinner,0)+0
    will return the Count, or a zero in the case of no match.

  3. #3
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    When I do the LEFT OUTER I end up with all 64 teams in my teams list, most of which have 0 wins and 0 losses. Is there a way to code in to not show a team if they have a zero in both columns?

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    to answer the question, you need to post the SQL for [Copy Of Qry_Tournaments_Sort vs Winner Count] and [Copy Of Qry_Tournaments_Sort vs Loser Count].

    Ideally, you just want to make sure that those queries return a zero record for the teams that never won/lost against a seed, and then do an inner join.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    When I do the LEFT OUTER I end up with all 64 teams in my teams list, most of which have 0 wins and 0 losses. Is there a way to code in to not show a team if they have a zero in both columns?
    That is what the Criteria row in the Query Builder is for!
    Give it a shot, and if you can't figure it out, post back.

  6. #6
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Thanks, that worked. Changed those individual queries to Left Join and then this query back to Inner Join and now I'm getting the 16 results like I am looking for.

  7. #7
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Upon closer inspection I am getting all the teams from the loser query but it's still leaving out the ones from the winner query with 0 losses.

  8. #8
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Here is my sql

    Winner Query:
    SELECT Tbl_Teams.Team, Count([Qry_Tournaments_Sort Loser].Winner) AS CountOfWinner
    FROM Tbl_Teams LEFT JOIN [Qry_Tournaments_Sort Loser] ON Tbl_Teams.Team = [Qry_Tournaments_Sort Loser].Winner
    GROUP BY Tbl_Teams.Team
    ORDER BY Count([Qry_Tournaments_Sort Loser].Winner) DESC;

    Loser Query:
    SELECT Tbl_Teams.Team, Count([Qry_Tournaments_Sort Winner].Loser) AS CountOfLoser
    FROM Tbl_Teams LEFT JOIN [Qry_Tournaments_Sort Winner] ON Tbl_Teams.Team = [Qry_Tournaments_Sort Winner].Loser
    GROUP BY Tbl_Teams.Team
    ORDER BY Count([Qry_Tournaments_Sort Winner].Loser) DESC;

    Combined:
    SELECT Tbl_Teams.Team, [Copy Of Qry_Tournaments_Sort vs Winner Count].CountOfWinner, [Copy Of Qry_Tournaments_Sort vs Loser Count].CountOfLoser
    FROM (Tbl_Teams INNER JOIN [Copy Of Qry_Tournaments_Sort vs Winner Count] ON Tbl_Teams.Team = [Copy Of Qry_Tournaments_Sort vs Winner Count].Team) INNER JOIN [Copy Of Qry_Tournaments_Sort vs Loser Count] ON Tbl_Teams.Team = [Copy Of Qry_Tournaments_Sort vs Loser Count].Team;

  9. #9
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Do you just have the one table?

    It be helpful to see its structure and same data.

  10. #10
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    The left join will get you the zeroes, so all you need to do is eliminate the teams with no wins and no losses.
    Code:
    Combined:
    SELECT 
      TT.Team, 
      TW.CountOfWinner, 
      TL.CountOfLoser
    FROM 
      (Tbl_Teams AS TT 
       INNER JOIN 
      [Copy Of Qry_Tournaments_Sort vs Winner Count] AS TW 
      ON TT.Team = TW.Team) 
      INNER JOIN 
      [Copy Of Qry_Tournaments_Sort vs Loser Count] AS TL 
      ON TT.Team = TL.Team
    WHERE ((TW.CountOfWinner + TL.CountOfLoser) > 0);

  11. #11
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    No, the main table I doing my queries off is my tournament table where I have the tournament, round, region, seed, etc.

    I can post that if there is no way to do the following. I changed the inner join to left outer which displayed all the teams. I then did the nz(count... to turn the blanks into zeros. Is there now a way to just eliminate the teams that have a zero in both the winner and loser column by entering something in the criteria?

  12. #12
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Is there now a way to just eliminate the teams that have a zero in both the winner and loser column by entering something in the criteria?
    Just place
    Code:
    <>0
    under the Criteria row under the one calculated column, and then place the same thing under the other on the second criteria row (do NOT place both on the same row).

    Basically, that is saying only return records if one of those two fields is not zero (so if they are both zero, they will be excluded).

  13. #13
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    57
    Quote Originally Posted by Dal Jeanis View Post
    The left join will get you the zeroes, so all you need to do is eliminate the teams with no wins and no losses.
    Code:
    Combined:
    SELECT 
      TT.Team, 
      TW.CountOfWinner, 
      TL.CountOfLoser
    FROM 
      (Tbl_Teams AS TT 
       INNER JOIN 
      [Copy Of Qry_Tournaments_Sort vs Winner Count] AS TW 
      ON TT.Team = TW.Team) 
      INNER JOIN 
      [Copy Of Qry_Tournaments_Sort vs Loser Count] AS TL 
      ON TT.Team = TL.Team
    WHERE ((TW.CountOfWinner + TL.CountOfLoser) > 0);
    Thanks for the code. This seems to be doing exactly what I wanted it to now.

  14. #14
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Great! Please mark thread solved. Top of page, under "thread tools".

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

Similar Threads

  1. Combining two queries.
    By Ray67 in forum Queries
    Replies: 1
    Last Post: 09-07-2012, 12:11 PM
  2. Combining queries..
    By Anthony88 in forum Queries
    Replies: 4
    Last Post: 05-02-2012, 02:46 PM
  3. combining two queries
    By camell in forum Queries
    Replies: 4
    Last Post: 03-04-2011, 02:41 PM
  4. Combining queries
    By wildlifeaccess in forum Queries
    Replies: 10
    Last Post: 09-20-2010, 07:35 AM
  5. Combining Two Queries
    By csolomon in forum Queries
    Replies: 1
    Last Post: 09-03-2009, 01:33 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