Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get scores from two separate bowling teams on one output table line.
Think i can do it with 2 separate select queries using joins as i showed below.

Input tables that i joined are
Schedule Table
DATE	    HOME TEAM #	      AWAY TEAM #
2017-02-13	     6	                7
2017-02-13	     3	                2
2017-02-13	     9           	    8
2017-02-13	     4	                5
2017-02-13	    10	                1


Data Table:
Date	TeamNum	Gm1	Gm2
12-09-16	4	726	811
12-09-16	6	540	573
12-09-16	5	450	480
12-09-16	7	640	497


Hope this is not too confusing.
Any help would be appreciated.
Thank you

What I have tried:

SELECT 'DATE','Match','TEAM', 'HmTeamNo' ,'Game 1', 'Game 2', 'Game 3', 'Total' , 'Away Tm' 
UNION (SELECT `MatchDate`, 
                   FORMAT(MatchNumber,0), 
       				TEAM, 
                   FORMAT(HmTeamNo,0), 
                   FORMAT(SUM(gm1),0), 
                   FORMAT(SUM(gm2),0), 
                   FORMAT(SUM(gm3),0), 
                   FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) as "Ttl" , 
                   FORMAT(AwayTeamNo,0) 
FROM Rainbow_Lanes_Matches 
      JOIN Rainbow_Lanes ON 
week = MatchWeek AND TEAMNUM = HmTeamNo 
WHERE MatchWeek =21
 			GROUP BY team 
ORDER BY MatchNumber
      
      )


Think it should work if i repeat the querie and change the hometeam variable to awayteam team and repeat the join but get errors.

UNION (SELECT `MatchDate`, 
                   FORMAT(MatchNumber,0), 
       				TEAM, 
                   FORMAT(HmTeamNo,0), 
                   FORMAT(SUM(gm1),0), 
                   FORMAT(SUM(gm2),0), 
                   FORMAT(SUM(gm3),0), 
                   FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) as "Ttl" , 
FROM Rainbow_Lanes_Matches 
      JOIN Rainbow_Lanes ON 
week = MatchWeek AND TEAMNUM = AwayTeamNo 
WHERE MatchWeek =21
 			GROUP BY team 
ORDER BY MatchNumber
      
      )


The second table includes the scores from the teams.


Output or Results Table will look like this:
DATE	Match	HmTm	 Gm1	 Gm2	   AwayTeam	         Gm1	Gm2	
13-02-17	1	4	     726	811	      9                  480	433
13-02-17	2	6	     540	573	      7	                 640	497
Posted
Updated 23-Feb-17 14:13pm

I would suggest using two subqueries, like what you have done. But, instead of using UNION, I would use a JOIN, because you need to treat them as two separate tables of data and only get what you need from each of them. Using UNION could show empty results for some records.

Try this query. The first SELECT will define your output, and the subqueries get the data you need for the home and away teams.

Hope this works for you - I'm not sure of your table structure so it might throw some initial syntax errors.

SELECT sub_home.MatchDate,
sub_home.MatchWeek,
sub_home.Team,
sub_home.HomeTeamNo,
sub_home.Game1,
sub_home.Game2,
sub_home.Game3,
sub_home.TotalScore
sub_away.AwayTeamNo,
sub_away.Game1,
sub_away.Game2,
sub_away.Game3,
sub_away.TotalScore
FROM (
	SELECT `MatchDate`, 
	FORMAT(MatchNumber,0) AS MatchNumber, 
	TEAM, 
	FORMAT(HmTeamNo,0) AS HomeTeamNo, 
	FORMAT(SUM(gm1),0) AS Game1, 
	FORMAT(SUM(gm2),0) AS Game2, 
	FORMAT(SUM(gm3),0) AS Game3, 
	FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) AS TotalScore
	FROM Rainbow_Lanes_Matches 
	JOIN Rainbow_Lanes ON week = MatchWeek AND TEAMNUM = HmTeamNo 
	GROUP BY team  
) sub_home
JOIN (
	SELECT `MatchDate`, 
	FORMAT(MatchNumber,0) AS MatchNumber, 
	TEAM, 
	FORMAT(AwayTeamNo,0) AS AwayTeamNo, 
	FORMAT(SUM(gm1),0) AS Game1, 
	FORMAT(SUM(gm2),0) AS Game2, 
	FORMAT(SUM(gm3),0) AS Game3, 
	FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) AS TotalScore 
	FROM Rainbow_Lanes_Matches 
	JOIN Rainbow_Lanes ON week = MatchWeek AND TEAMNUM = AwayTeamNo 
	GROUP BY team 
	ORDER BY MatchNumber
) sub_away
ON sub_home.matchnumber = sub_away.MatchNumber
AND sub_home.MatchWeek = sub_away.MatchWeek;
 
Share this answer
 
v2
Comments
Member 12995602 22-Feb-17 6:23am    
Thanks for your input!
It worked great on data that was set at week = 21.
This was used to test the data as you probably know.

When i removed those lines then got rows doubling up, and values are summing incorrectly?

I am testing but it looks good to me so far.
Any ideas why this does not work when looking at more than one week?

MatchDate MatchNumber HomeTeamNo Game1 Game2 Game3 TotalScore AwayTeamNo Game1 Game2 Game3 TotalScore
2016-12-19 1 8 1,288 1,223 1,248 3,759 4 1,581 1,682 1,529 4,792
2016-12-19 1 8 1,288 1,223 1,248 3,759 5 2,154 2,114 2,151 6,419
2017-01-16 4 4 3,443 3,862 3,841 11,146 3 1,740 1,803 1,696 5,239
2017-01-16 4 4 3,443 3,862 3,841 11,146 2 4,326 4,238 4,196 12,760
2017-01-16 4 4 3,443 3,862 3,841 11,146 9 784 826 699 2,309
2017-01-16 2 6 1,646 1,775 1,705 5,126 8 3,087 3,116 3,403 9,606
2017-01-16 2 6 1,646 1,775 1,705 5,126 7 1,776 2,051 1,977 5,804
[no name] 22-Feb-17 13:19pm    
I think it's because for a single MatchNumber (e.g. MatchNumber 1), there are multiple HomeTeam-AwayTeam records.

I just updated the above query. Could you try it again? I just updated it to join on MatchWeek as well.

Does the data now look correct?
Member 12995602 23-Feb-17 20:13pm    
Thanks for help.
I was able to get it working the way i wanted while testing in phpMyadmin.
Although when transferring code to "tabulizer" on my website it would not show the items on the right side (away team) or second query.
Possibly I cannot use "views" or joins?
It would not display anything to right of "away team".
Will be looking into another way to redesign same thing, maybe using temp tables without joins?
thanks again for your help.
Ray
SELECT  "Date", "Wk", "Match", "Hm Team", "HDCP", "Gm 1", "Gm 2", "Gm 3", "Ttl","Away Team","HDCP", "Gm 1", "Gm 2", "Gm 3", "Ttl"   
UNION     

(SELECT 	
sub_home.MatchDate,
sub_home.MatchWeek,
sub_home.Matchnumber,
sub_home.HomeTeamNo,
sub_home.HDCP,
/*  sub_home.Bowler,  */
sub_home.Game1,
sub_home.Game2,
sub_home.Game3,
sub_home.TotalScore,

sub_away.AwayTeamNo,
sub_away.HDCP,
sub_away.Game1,
sub_away.Game2,
sub_away.Game3,
sub_away.TotalScore

 FROM 
(SELECT 
	`MatchDate`, 
 	FORMAT(MatchWeek,0) AS MatchWeek, 	
 	FORMAT(MatchNumber,0) AS MatchNumber, 
	TEAM, 
	FORMAT(HmTeamNo,0) AS HomeTeamNo, 

FORMAT ((250 -( SELECT SUM(Gm1+Gm2+Gm3) / ( SELECT COUNT(Gm1)*3)*0.8)),0) AS HDCP,
 
 
        FORMAT(SUM(gm1),0) AS Game1, 
	FORMAT(SUM(gm2),0) AS Game2, 
	FORMAT(SUM(gm3),0) AS Game3, 
	FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) AS TotalScore
	FROM Rainbow_Lanes_Matches 
	JOIN Rainbow_Lanes ON week = MatchWeek 
		AND TEAMNUM = HmTeamNo 
 									WHERE `Week` >= 19 
	GROUP BY `Match_id` 

 ORDER BY `MatchWeek` AND `MatchNumber`
) 
sub_home
JOIN (SELECT 
	`MatchDate`, 
 	FORMAT(MatchWeek,0) AS MatchWeek, 
    FORMAT(MatchNumber,0) AS MatchNumber, 
	TEAM, 
	FORMAT(AwayTeamNo,0) AS AwayTeamNo, 
FORMAT ((250 -( SELECT SUM(Gm1+Gm2+Gm3) / ( SELECT COUNT(Gm1)*3)*0.8)),0) AS HDCP,
      FORMAT(SUM(gm1),0) AS Game1, 
	FORMAT(SUM(gm2),0) AS Game2, 
	FORMAT(SUM(gm3),0) AS Game3, 
	FORMAT((SUM(gm1)+SUM(gm2)+SUM(gm3)),0) AS TotalScore 
	FROM Rainbow_Lanes_Matches 
	JOIN Rainbow_Lanes ON week = MatchWeek 
      AND TEAMNUM = AwayTeamNo
       								WHERE `Week` >= 19 
	GROUP BY `Match_id`
	ORDER BY `MatchWeek` AND `MatchNumber`
) 

sub_away
ON sub_home.matchnumber = sub_away.MatchNumber
AND sub_home.MatchWeek = sub_away.MatchWeek);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900