连接/联合?

9rygscc1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(190)

我有两个查询,基本上分组主队为选定的一个月,以获得他们的统计数据,然后其他组客场球队为选定的一个月,以获得他们的统计数据,但第三个查询,我想要一个整体的统计数据集,所以这两个组合,但我遇到了砖墙与我的一切尝试!

SELECT HomeTeam,
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END) AS Played,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) Won,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Draw' THEN 1 ELSE 0 END) Draw,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Away' THEN 1 ELSE 0 END) Lost,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) / 
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END),2) AS StrikeRate,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lsph ELSE 0 END),2) AS WinLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspd ELSE 0 END),2) AS DrawLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspa ELSE 0 END),2) AS LoseLSP
FROM Results_Football
GROUP BY HomeTeam
HAVING Played >= 20 AND WinLSP > 0
ORDER BY StrikeRate DESC
LIMIT 10

SELECT AwayTeam,
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END) AS Played,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Away' THEN 1 ELSE 0 END) Won,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Draw' THEN 1 ELSE 0 END) Draw,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) Lost,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) / 
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END),2) AS StrikeRate,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspa ELSE 0 END),2) AS WinLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspd ELSE 0 END),2) AS DrawLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lsph ELSE 0 END),2) AS LoseLSP
FROM Results_Football
GROUP BY AwayTeam
HAVING Played >= 20 AND WinLSP > 0
ORDER BY StrikeRate DESC
LIMIT 10

所以基本上我要做的是按团队分组,同时得到他们的统计数据,如果有办法将这两个查询结合起来实现这一点的话?
提前谢谢!

相关问题