到目前为止,我已经写了一份精选陈述,以获取球队名称,主场比赛的平均得分以及他们对另一支球队的平均得分。
当他们离开时,我还有另一支球队,我在这里选择了离开的球队,他们离开时的平均得分和他们承认的平均值。
如何从选择查询中加入这两个表,以便获得一个包含团队名称,平均得分主场,平均失球主场,平均失球率和平均得分的表?
这是我的代码:
select home_team, avg(home_score_half + home_score_full) as avg_scored_home,
avg(away_score_half + away_score_full) as avg_conceded_home
from matches
group by home_team;
select away_team, avg(away_score_half + away_score_full) as avg_scored_away,
avg(home_score_half + home_score_full) as avg_conceded_away
from matches
group by away_team;
任何帮助将不胜感激。谢谢!
答案 0 :(得分:3)
如果您希望结果加入(每个团队在同一行上),则可以将两个查询用作加入的子查询
select t1.home_team
, t1.avg_scored_home
, t1.avg_conceded_home
, t2.away_team
, t2.avg_scored_away
, t2.avg_conceded_away
from (
select home_team
, avg(home_score_half + home_score_full) as avg_scored_home
, avg(away_score_half + away_score_full) as avg_conceded_home
from matches
group by home_team
) t1
INNER JOIN (
select away_team
, avg(away_score_half + away_score_full) as avg_scored_away
, avg(home_score_half + home_score_full) as avg_conceded_away
from matches group by away_team
) t2 ON t1.home_team = t2.away_team