I have a table set up as follows:
killerId | killId
1 | 4
1 | 4
2 | 1
3 | 4
4 | 2
5 | 2
1 | 6
This essentially is a killfeed that shows who killed who for a game I am working on.
I want to be able to generate a query that will allow me to give players the ability to see who has killed them the most.
The response ideally would be something like:
killerId | mostKilledId | timesKilled
1 | 4 | 2
2 | 1 | 1
3 | 4 | 1
4 | 2 | 2
5 | 2 | 2
For my use case, I need to see a list of all players and who their "nemesis" is (the person who has killed them the most).
I have:
SELECT DISTINCT killerId, killedId, COUNT(*) FROM kills GROUP BY killerId, killedId;
Which gets me the following response:
killerId | killedId | count
1 | 4 | 2
2 | 1 | 1
3 | 4 | 1
4 | 2 | 1
5 | 2 | 1
1 | 5 | 1
I do not want to see duplicate killerId's here and would instead prefer to ONLY show the the killerId and killedId that has the highest count. In the example above, I don't want to see the final kill because it is not higher than the 2 kills on id 4.
EDIT: Per answers in the comments, I've got:
SELECT a.killerId, a.killedId, MAX(a.count)
FROM (SELECT killerId, killedId, COUNT(*) as count FROM kills GROUP BY killerId, killedId) AS a
INNER JOIN(SELECT killerId, killedId, MAX(count) count
FROM (SELECT killerId, killedId, COUNT(*) as count FROM kills GROUP BY killerId, killedId) AS c
GROUP BY c.killerId, c.killedId) AS b
ON a.killerId = b.killerId AND a.killedId = b.killedId
GROUP BY a.killerId, a.killedId;
However I am still getting the same results as earlier and I am not feeling as if I am understanding what is happening here/what the right path forward is.