嵌套分组 mysql
Nested grouping mysql
我有一个 table results
,其中包含以下重要列:
f_team1
(VARCHAR)
f_team2
(VARCHAR)
f_player1
(VARCHAR)
f_player2
(VARCHAR)
f_total_ftg
(整数)
一个 team
可以在 f_team1
或 f_team2
列中
一个 player
可以在 f_player1
或 f_player2
列中
我正在尝试为每个玩家找到 f_total_ftg 的 AVG,然后获取该 AVG 并将其应用于每个团队。所以 team_average
将是所有球员平均得分的总和。
我的尝试:
SELECT team,
(
SELECT AVG(player_team_average) AS players_team_average
FROM
(
SELECT AVG(CASE WHEN f_team1='arsenal' OR f_team2='arsenal' THEN f_total_ftg END) AS team_average
FROM
(
SELECT f_player1 AS player, f_team1, f_team2, f_total_ftg FROM results
UNION
SELECT f_player2 AS player, f_team1, f_team2, f_total_ftg FROM results
) x GROUP BY player
) x
) AS team_average FROM (
(SELECT f_team1 AS team, f_total_ftg FROM results)
UNION
(SELECT f_team2 AS team, f_total_ftg FROM results)
) x GROUP BY team
当前输出:
正如您从我的代码中看到的那样,我将球队设为 arsenal
,这给出了不同球员的阿森纳的正确总体平均值。我怎样才能为每个团队做到这一点?我需要分出字符串并应用每个团队的平均值。
我该怎么做?
Table 看起来像这样:
这是你想要的吗?
select team, sum(avg_ftg) sum_avg_ftg
from (
select team, player, avg(f_total_ftg) avg_ftg
from (
select f_team1 team, f_player1 player, f_total_ftg from mytable
union all select f_team2, f_player2, f_total_ftg from mytable
) t
group by team, player
) t
group by team
这会将 team/players 元组转为行,然后计算每个球员和球队的平均 ftg,最后计算每个球队的平均 ftg。
在 MySQL 的最新版本中,这可以用横向连接更有效地表达:
select team, sum(avg_ftg) sum_avg_ftg
from (
select x.team, x.player, avg(t.f_total_ftg) avg_ftg
from mytable t
cross join lateral (
select t.f_team1 team, t.f_player1 player
union all select t.f_team2, t.f_player2
) x
group by x.team, x.player
) t
group by team
我有一个 table results
,其中包含以下重要列:
f_team1
(VARCHAR)f_team2
(VARCHAR)f_player1
(VARCHAR)f_player2
(VARCHAR)f_total_ftg
(整数)
一个 team
可以在 f_team1
或 f_team2
列中
一个 player
可以在 f_player1
或 f_player2
列中
我正在尝试为每个玩家找到 f_total_ftg 的 AVG,然后获取该 AVG 并将其应用于每个团队。所以 team_average
将是所有球员平均得分的总和。
我的尝试:
SELECT team,
(
SELECT AVG(player_team_average) AS players_team_average
FROM
(
SELECT AVG(CASE WHEN f_team1='arsenal' OR f_team2='arsenal' THEN f_total_ftg END) AS team_average
FROM
(
SELECT f_player1 AS player, f_team1, f_team2, f_total_ftg FROM results
UNION
SELECT f_player2 AS player, f_team1, f_team2, f_total_ftg FROM results
) x GROUP BY player
) x
) AS team_average FROM (
(SELECT f_team1 AS team, f_total_ftg FROM results)
UNION
(SELECT f_team2 AS team, f_total_ftg FROM results)
) x GROUP BY team
当前输出:
正如您从我的代码中看到的那样,我将球队设为 arsenal
,这给出了不同球员的阿森纳的正确总体平均值。我怎样才能为每个团队做到这一点?我需要分出字符串并应用每个团队的平均值。
我该怎么做?
Table 看起来像这样:
这是你想要的吗?
select team, sum(avg_ftg) sum_avg_ftg
from (
select team, player, avg(f_total_ftg) avg_ftg
from (
select f_team1 team, f_player1 player, f_total_ftg from mytable
union all select f_team2, f_player2, f_total_ftg from mytable
) t
group by team, player
) t
group by team
这会将 team/players 元组转为行,然后计算每个球员和球队的平均 ftg,最后计算每个球队的平均 ftg。
在 MySQL 的最新版本中,这可以用横向连接更有效地表达:
select team, sum(avg_ftg) sum_avg_ftg
from (
select x.team, x.player, avg(t.f_total_ftg) avg_ftg
from mytable t
cross join lateral (
select t.f_team1 team, t.f_player1 player
union all select t.f_team2, t.f_player2
) x
group by x.team, x.player
) t
group by team