我有以下数据问题,我试图找出如何封装在Google表格公式或查询中:
Player Game Shots Scored
Washington 1 3
Lincoln 1 6
Roosevelt 1 4
Washington 2 3
Grant 2 1
Kennedy 2 3
我感兴趣的是计算平均得分作为该游戏中得分最高的球员的百分比。或者,换句话说:
For a Player P:
Return the Average of(
For Each Distinct Game containing P (
P.Points / Max(Points)
)
)
例如,使用上面的数据,我正在查看的结果是:
Player Performance
Washington 75% [=(3/6+3/3)/2 * 100%]
Lincoln 100%
Roosevelt 66%
Grant 33%
Kennedy 100%
尽管这种复合计算被证明很难翻译成Google查询语言,但是有帮助吗?
=独特(过滤器(A2:C,A2:A =“华盛顿”))
返回“华盛顿”玩家的所有行,但(可能)丢失该游戏中得分最高的人的数据。是否也可以在过滤器中包含该行?
您可以通过在Google Apps脚本中创建的custom function完成此操作。为此,请按照下列步骤操作:
function GETAVERAGES(input) {
const games = Array.from(new Set(input.map(row => row[1]))).map(game => {
let maxResult = Math.max(...input.filter(row => row[1] === game).map(row => row[2]));
return [game, maxResult]; // Get max score for each game
});
return Array.from(new Set(input.map(row => row[0]))).map(player => {
let playerRows = input.filter(row => row[0] === player); // Filter rows according to player
let percentages = playerRows.reduce((acc, row) => { // Percentage sum for current player
let maxGame = games.find(game => game[0] === row[1])[1]; // Max score for current game
return acc + row[2] / maxGame; // Accumulated percentage
}, 0);
let average = percentages / playerRows.length; // Divide accumulated per number of games
return [player, average]; // Return array of player name and corresponding average
});
}
A2:C7
),如下所示: