从每小时总和中获取最大用户分数

时间:2012-12-14 02:13:18

标签: mysql sql group-by sum

我有一个包含如下示例数据的MySql表:

+---------+---------+--------+---------------------+
|      id | user_id | scores |          created_at |
+---------+---------+--------+---------------------+
|       1 |       1 |     10 | 2012-12-14 02:40:37 |
|       2 |       1 |     20 | 2012-12-14 02:55:54 |
|       3 |       1 |     10 | 2012-12-14 01:17:21 |
|       4 |       2 |     30 | 2012-12-13 01:54:47 |
|       5 |       2 |     55 | 2012-12-15 00:34:39 |
|       6 |       2 |     10 | 2012-12-14 00:20:21 |
+---------+---------+--------+---------------------+

我需要查询它,以便每个用户和每小时对得分进行求和。假设这里的一小时是created_at,跳过分钟和秒(04:00:00到04:59:59等)。所以像这样:

+---------+--------+---------------------+
| user_id | scores |          created_at |
+---------+--------+---------------------+
|       1 |     30 | 2012-12-14 02:00:00 |
|       1 |     10 | 2012-12-14 01:00:00 |
|       2 |     30 | 2012-12-13 01:00:00 |
|       2 |     55 | 2012-12-15 00:00:00 |
|       2 |     10 | 2012-12-14 00:00:00 |
+---------+--------+---------------------+

在此示例数据中,只有第一位用户在一小时内(2012-12-14 02:00:00)播放了多次 - 所以他在那个小时内的分数总结了。

从总计分数来看,我只需要为每个用户提供最高分(创建排名)。所以最终的预期结果应该是:

+---------+---------------------+---------------------+
| user_id | top_scores_per_hour |                hour |
+---------+---------------------+---------------------+
|       1 |                  30 | 2012-12-14 02:00:00 |
|       2 |                  55 | 2012-12-15 00:00:00 |
+---------+---------------------+---------------------+

我有一个想法如何做到这一点......我可以在数据库之外处理休息,但我真的很想知道 - 我怎么能用SQL查询呢?

3 个答案:

答案 0 :(得分:1)

SELECT a.user_id,
       a.totalScores top_scores_per_hour,
       a.newTime hour
FROM
    (
      SELECT  user_id, 
              SUM(Scores) totalScores, 
              DATE_Format(created_at, '%Y-%m-%d %H:00:00') newTime
      FROM    TableName 
      GROUP BY user_id, DATE_Format(created_at, '%y-%m-%d %H:00:00')
    ) a INNER JOIN 
    (
      SELECT  user_id, 
              max(DATE_Format(created_at, '%Y-%m-%d %H:00:00')) newTime
      FROM    TableName 
      GROUP BY user_ID
    ) b ON a.user_ID = b.user_ID AND
           a.newTime = b.newTime

输出

╔═════════╦═════════════════════╦═════════════════════╗
║ USER_ID ║ TOP_SCORES_PER_HOUR ║        HOUR         ║
╠═════════╬═════════════════════╬═════════════════════╣
║       1 ║                  30 ║ 2012-12-14 02:00:00 ║
║       2 ║                  55 ║ 2012-12-15 00:00:00 ║
╚═════════╩═════════════════════╩═════════════════════╝

答案 1 :(得分:1)

试试这个:

SELECT user_id, score, createdDate 
FROM (SELECT user_id, SUM(scores) score, DATE_FORMAT(created_at, '%Y-%m-%d %H:00:00') createdDate 
      FROM tablename 
      GROUP BY user_id, createdDate
      ORDER BY user_id, score DESC) AS A 
GROUP BY user_id

答案 2 :(得分:0)

怎么样

Select user_id, Max(totalHouirlyScore) TopHourlyScore
From (Select user_id, Sum(scores) totalHouirlyScore
      From tablename
      Group By user_id, 
            DateFormat(created_at, '%y%m%d%H')) Z