Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join two tables 1

Status
Not open for further replies.

djhawthorn

Technical User
Mar 4, 2002
641
AU
I have the following query:

SELECT Team1, COUNT(Winner) AS Won FROM Games WHERE Winner = -1 GROUP BY Team1
UNION ALL
SELECT Team2, COUNT(Winner) AS Won FROM Games WHERE Winner = -1 GROUP BY Team2;

Which returns the following:

Code:
+-------+-----+
| Team1 | Won |
+-------+-----+
|     1 |   1 |
|     9 |   1 |
|     4 |   1 |
|     9 |   1 |
+-------+-----+

I need it to merge the Team1 column though to get the following output:

Code:
+-------+-----+
| Team1 | Won |
+-------+-----+
|     1 |   1 |
|     9 |   2 |
|     4 |   1 |
+-------+-----+

Can someone help?
Cheers.

[ponder][laughtears] The dumber they think you are, the more surprised they'll be when you kill them! [machinegun][rofl2]
 
Code:
SELECT Team1    AS Team
     , SUM(Won) AS Won
  FROM ( [i]put your UNION query here[/i] ) AS dt
GROUP
    BY Team1

r937.com | rudy.ca
 
Sweet.

So now I need to merge that with another query:

SELECT Winner AS TeamWon, COUNT(Winner) AS GamesWon
FROM Games
WHERE Winner > 0
GROUP BY TeamWon

Which returns something like:

Code:
+---------+----------+
| TeamWon | GamesWon |
+---------+----------+
|       1 |        3 |
|       2 |        3 |
|       3 |        1 |
|       4 |        1 |
|       5 |        1 |
|       6 |        1 |
|       7 |        2 |
|       8 |        1 |
|       9 |        1 |
+---------+----------+

I've tried various combinations of UNION and nested SELECTS, but cant get it to work. I think I may need to JOIN them but cant seem to get the syntax right...

Thanks again

[ponder][laughtears] The dumber they think you are, the more surprised they'll be when you kill them! [machinegun][rofl2]
 
Nevermind - worked it out.

I had the LEFT JOIN around the wrong way - thanks for the help.

[ponder][laughtears] The dumber they think you are, the more surprised they'll be when you kill them! [machinegun][rofl2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top