我有一张桌子给玩家:
set.seed(1234)
a <- factor(c("ID1","ID2","ID3","ID4","ID5"))
b <- runif(5)
b1 <- runif(5)
b2 <- runif(5)
b3 <- runif(5)
c1 <- runif(5)
c2 <- runif(5)
c3 <- runif(5)
df <- data.frame(a,b1,b2,b3,c1,c2,c3)
我正在尝试根据player_id分组的出现次数对它们进行排名。
这就是我的尝试:
+------------+-----------+
| player_id | date |
+------------+-----------+
| 1 | 27/1/2018 |
| 1 | 27/1/2018 |
| 1 | 27/1/2018 |
| 2 | 27/1/2018 |
| 2 | 27/1/2018 |
| 3 | 27/1/2018 |
+------------+-----------+
结果我得到了:
SELECT total2.player_id, COUNT(*) + 1 AS rank FROM (
SELECT player_id, COUNT(*) AS total
FROM players
GROUP BY player_id
) total2
INNER JOIN (
SELECT player_id, COUNT(*) AS total
FROM players
GROUP BY player_id
) total3 ON total3.total > total2.total
GROUP BY total2.player_id
结果我想要:
+------------+------+
| player_id | rank |
+------------+------+
| 2 | 2 |
| 3 | 3 |
+------------+------+
答案 0 :(得分:1)
试试这个
这里是小提琴
SELECT a.player_id, @row_number:=@row_number+1 AS row_number
FROM (SELECT player_id, COUNT(*) AS total
FROM players
GROUP BY player_id) a,
(SELECT @row_number:=0) AS t
ORDER BY TOTAL desc