我有下面的SQL表,我们称其为teams_score,它显示了一个游戏的2个玩家的团队及其得分。每当他们出现在球员组中时,我需要观察每个球员的得分。我认为这就像融化。
Player1, Player2, TeamScore
"Johnny Onspot", "Pat Magroin", 95
"Pepe Roni", "Cole Kutz", 78
"Ben Dover", "Ibin Yerkinoff", 76
"Johnny Onspot", "Frumunda Mabalz", 69
"Sal Lami", "Cole Kutz", 65
"Pat Magroin", "Frumunda Mabalz", 63
我想找到平均得分最高的前三名球员。
例如,“ Pat Magroin”出现在2支球队中,得分分别为95和63,平均得分为79
因此,我需要显示下表“ player_score”,然后按平均得分进行汇总。我如何在下表中显示player_score?
Player, Score
"Ben Dover", 76
"Cole Kutz", 78
"Cole Kutz", 65
"Frumunda Mabalz", 69
"Frumunda Mabalz", 63
"Ibin Yerkinoff", 76
"Johnny Onspot", 95
"Johnny Onspot", 69
"Pat Magroin", 95
"Pat Magroin", 63
"Pepe Roni", 78
"Sal Lami", 65
一旦有了player_score,我应该能够运行查询(如下所示)以获取表result_table,如下所示。
SELECT Player, AVG(Score) AS Avg_Score FROM player_score
GROUP BY Player
ORDER BY Avg_Score DESC
LIMIT 3;
Player, Avg_Score
"Johnny Onspot", 82
"Pat Magroin", 79
"Pepe Roni", 78
答案 0 :(得分:1)
SELECT *
FROM (
SELECT Player1 AS Player, TeamScore as Score
FROM teams_score
UNION ALL
SELECT Player2 AS Player, TeamScore as Score
FROM teams_score
)
应该给
Player Score
"Johnny Onspot", 95
"Pat Magroin", 95
"Pepe Roni", 78
"Cole Kutz", 78
"Ben Dover", 76
"Ibin Yerkinoff", 76
"Johnny Onspot", 69
"Frumunda Mabalz", 69
"Sal Lami", 65
"Cole Kutz", 65
"Pat Magroin", 63
"Frumunda Mabalz", 63
您应该可以根据自己的意愿做
WITH
...above code
AS player_score
SELECT Player, AVG(Score) AS Avg_Score
FROM player_score
GROUP BY Player
ORDER BY Avg_Score DESC
LIMIT 3;
答案 1 :(得分:0)
您可以使用UNION ALL
:
SELECT Player, AVG(score) AS avg_score
FROM(SELECT Player1 AS player, Score FROM player_score
UNION ALL SELECT Player2, Score FROM player_score) sub
GROUP BY Player
ORDER BY avg_Score DESC
LIMIT 3;
答案 2 :(得分:0)
CREATE TABLE player_score AS SELECT Player1 AS Player, TeamScore AS Score FROM(
SELECT Player1, TeamScore
FROM teams_score
UNION ALL
SELECT Player2, TeamScore
FROM teams_score);
-- Delete duplicate rows where player and score are the same in both rows
DELETE FROM player_score WHERE rowid NOT IN
(SELECT min(rowid) FROM player_score GROUP BY Player, Score);