获取特定人员列中出现频率最高的值
Getting the most frequent value in a column for a specific person
所以我有一个 table 来收集每个人的数据。
+--------+-------+--------+---------+----------------+
| player | kills | deaths | assists | champSelection |
+--------+-------+--------+---------+----------------+
| Bob | 1 | 3 | 4 | Horse |
+--------+-------+--------+---------+----------------+
| Bob | 2 | 7 | 5 | Horse |
+--------+-------+--------+---------+----------------+
| Jake | 5 | 5 | 5 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 2 | 3 | 4 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 1 | 1 | 9 | Horse |
+--------+-------+--------+---------+----------------+
| Marie | 6 | 7 | 2 | Dog |
+--------+-------+--------+---------+----------------+
而我 运行
SELECT player, sum(kills), sum(deaths), sum(assists)
FROM playerTable
GROUP BY player
ORDER BY player ASC;
我如何在我的查询中添加他们最常用的 champSelection 是什么?我正在尝试将我的 table 显示为
+--------+-------+--------+---------+----------------+
| player | kills | deaths | assists | champSelection |
+--------+-------+--------+---------+----------------+
| Bob | 3 | 10 | 9 | Horse |
+--------+-------+--------+---------+----------------+
| Jake | 5 | 5 | 5 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 9 | 11 | 15 | Dog |
+--------+-------+--------+---------+----------------+
试试这个:
SELECT player, SUM(kills), SUM(deaths), SUM(assists), frequency
FROM
(
SELECT player, SUM(kills) AS kills, SUM(deaths) AS deaths, SUM(assists) AS assists, champSelection, COUNT(*) AS frequency
FROM playerTable
GROUP BY player, champSelection
ORDER BY frequency DESC
) AS inner_table
GROUP BY player
所以我有一个 table 来收集每个人的数据。
+--------+-------+--------+---------+----------------+
| player | kills | deaths | assists | champSelection |
+--------+-------+--------+---------+----------------+
| Bob | 1 | 3 | 4 | Horse |
+--------+-------+--------+---------+----------------+
| Bob | 2 | 7 | 5 | Horse |
+--------+-------+--------+---------+----------------+
| Jake | 5 | 5 | 5 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 2 | 3 | 4 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 1 | 1 | 9 | Horse |
+--------+-------+--------+---------+----------------+
| Marie | 6 | 7 | 2 | Dog |
+--------+-------+--------+---------+----------------+
而我 运行
SELECT player, sum(kills), sum(deaths), sum(assists)
FROM playerTable
GROUP BY player
ORDER BY player ASC;
我如何在我的查询中添加他们最常用的 champSelection 是什么?我正在尝试将我的 table 显示为
+--------+-------+--------+---------+----------------+
| player | kills | deaths | assists | champSelection |
+--------+-------+--------+---------+----------------+
| Bob | 3 | 10 | 9 | Horse |
+--------+-------+--------+---------+----------------+
| Jake | 5 | 5 | 5 | Dog |
+--------+-------+--------+---------+----------------+
| Marie | 9 | 11 | 15 | Dog |
+--------+-------+--------+---------+----------------+
试试这个:
SELECT player, SUM(kills), SUM(deaths), SUM(assists), frequency
FROM
(
SELECT player, SUM(kills) AS kills, SUM(deaths) AS deaths, SUM(assists) AS assists, champSelection, COUNT(*) AS frequency
FROM playerTable
GROUP BY player, champSelection
ORDER BY frequency DESC
) AS inner_table
GROUP BY player