我有一个具有这种结构的表:
score_id | user_id | category | subcategory | score
1 | 1 | game | 0 | 100
2 | 1 | game | 0 | 200
3 | 1 | quiz | 0 | 2000
4 | 1 | quiz | 1 | 1000
5 | 1 | game | 0 | 10
6 | 1 | game | 1 | 10
7 | 1 | game | 2 | 100
8 | 1 | game | 1 | 500
9 | 2 | game | 0 | 600
我需要不同的查询mysql:
1)所有团队的排名按user_id分类所有最佳结果的总和,用户类别和子类别的所有记录
预期结果
user_id | total_score | ranking
1 | 3310 | 1
2 | 600 | 2
,其中
3310 = (200 + 10 + 100+1000+2000)
200 is the best result of game 0,
10 is the best result of game 1,
100 is the best result of game 2,
2000 is the best result of quiz 0
1000 is the best result of quiz 1
600 = (600)
600 is the best result of game 0,
@Strawberry的解决方案(谢谢)
SELECT a.*
, @i:=@i+1 rank
FROM
( SELECT user_id
, SUM(subtotal) total
FROM
( SELECT user_id
, category
, subcategory
, MAX(score) subtotal
FROM my_table
GROUP
BY user_id
, category
, subcategory
) x
GROUP
BY user_id
) a
JOIN
( SELECT @i:=0) vars
ORDER
BY total DESC;
2)所有团队的排名按user_id排序所有最佳结果的总和,用于所有类别和子类别的记录仅用于游戏或测验
预期结果游戏
user_id | total_score | ranking
2 | 600 | 1
1 | 310 | 2
其中
600 = (600)
600 is the best result of game 0,
310 = (200 + 10 + 100)
200 is the best result of game 0,
10 is the best result of game 1,
100 is the best result of game 2,
预期结果测验
user_id | total_score | ranking
1 | 3000 | 1
2 | 0 | 2
其中
3000 = (200 + 10 + 100)
2000 is the best result of quiz 0
1000 is the best result of quiz 1
0= (0)
(user_id= 2 don't play quiz)
基于@Strawberry的解决方案(谢谢)
SELECT a.*
, @i:=@i+1 rank
FROM
( SELECT user_id
, SUM(subtotal) total
FROM
( SELECT user_id
, category
, subcategory
, MAX(score) subtotal
FROM my_table
WHERE category = 'game' // or 'quiz
GROUP
BY user_id
, category
, subcategory
) x
GROUP
BY user_id
) a
JOIN
( SELECT @i:=0) vars
ORDER
BY total DESC;
3)所有团队的排名按user_id排序所有最佳结果的总和,用于所有类别和子类别的用户记录仅适用于具有特定子类别的游戏或测验
预期结果游戏0
user_id | total_score | ranking
2 | 600 | 1
1 | 310 | 2
其中
600 = (600)
600 is the best result of game 0,
200 = (200 )
200 is the best result of game 0,
基于@Strawberry的解决方案(谢谢)
SELECT a.*
, @i:=@i+1 rank
FROM
( SELECT user_id
, SUM(subtotal) total
FROM
( SELECT user_id
, category
, subcategory
, MAX(score) subtotal
FROM my_table
WHERE category = 'game'
AND subcategory = '0'
GROUP
BY user_id
, category
, subcategory
) x
GROUP
BY user_id
) a
JOIN
( SELECT @i:=0) vars
ORDER
BY total DESC;
4)获得查询1的单个用户的总分(ex user_id = 1)
5)获得查询1的单个用户(ex user_id = 1)的排名
6)获得查询2的单个用户的总分(ex user_id = 1)
7)获得查询2的单个用户(ex user_id = 1)的排名
8)获得查询3的单个用户的总分(ex user_id = 1)
9)获得查询3的单个用户(ex user_id = 1)的排名
谢谢你!答案 0 :(得分:1)
这是第一个。使用此功能,请向我们展示您为其余部分所做的最佳努力......
In [184]: s[4:] / s[:-4].values - 1
Out[184]:
2000-01-01 04:00:00 inf
2000-01-01 05:00:00 4.000000
2000-01-01 06:00:00 2.000000
Freq: H, dtype: float64
请注意,此解决方案不考虑关系。