我正在尝试计算多列中的出现次数,然后输出结果并根据计算对它们进行排序。
计算killer_uid发生的次数。然后计算他们的ID在victim_uid中出现的次数。然后根据killer_uid / victim_uid的计数输出结果。请参阅下面的示例表和所需的输出。
temp = []
for i in obs:
for j in i:
temp.extend(j)
temp = np.array(temp)
print(temp.shape)
(3,4)
期望的输出: killer_uid为player_id, 计算killer_uid AS player_kills和victim_uid AS player_deaths的实例, 然后除以player_kills / player_deaths(如果除以0则返回0)并按这些结果DESC排序。
Table = kill_feed
+------------+------------+
| killer_uid | victim_uid |
+------------+------------+
| 1 | 2 |
| 2 | 3 |
| 2 | 3 |
| 2 | 1 |
| 2 | 1 |
| 1 | 1 |
+------------+------------+
我尝试使用的语句但是抛出了语法错误。
+-----------+--------------+---------------+
| player_id | player_kills | player_deaths |
+-----------+--------------+---------------+
| 2 | 4 | 1 |
| 1 | 2 | 3 |
| 3 | 0 | 2 |
+-----------+--------------+---------------+
Order by player_kills / player_deaths
1st = 2 since 4/1 = 4
2nd = 1 since 2/3 = 0.66
3rd = 3 since 0/2 = 0
答案 0 :(得分:1)
E.g:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,killer_uid INT NOT NULL
,victim_uid INT NOT NULL
);
INSERT INTO my_table (killer_uid,victim_uid) VALUES
(1,2),
(2,3),
(2,3),
(2,1),
(2,1),
(1,1);
SELECT player_id
, SUM(x) player_kills
, SUM(y) player_deaths
FROM
( SELECT killer_uid player_id
, 1 x
, 0 y
FROM my_table
UNION ALL
SELECT victim_uid
, 0
, 1
FROM my_table
) x
GROUP
BY player_id
ORDER
BY COALESCE(SUM(x)/SUM(y),SUM(x)) DESC;
+-----------+--------------+---------------+
| player_id | player_kills | player_deaths |
+-----------+--------------+---------------+
| 2 | 4 | 1 |
| 1 | 2 | 3 |
| 3 | 0 | 2 |
+-----------+--------------+---------------+