2

我有一个这样的 MYSQL 表:

  id |  userid  |  score  |      datestamp      |
-----------------------------------------------------
  1  |    1     |   5     |  2012-12-06 03:55:16
  2  |    2     |   0,5   |  2012-12-06 04:25:21
  3  |    1     |   7     |  2012-12-06 04:35:33
  4  |    3     |   12    |  2012-12-06 04:55:45
  5  |    2     |   22    |  2012-12-06 05:25:11
  6  |    1     |   16,5  |  2012-12-06 05:55:21
  7  |    1     |   19    |  2012-12-06 13:55:16
  8  |    2     |   8,5   |  2012-12-07 06:27:16
  9  |    2     |   7,5   |  2012-12-07 08:33:16
  10 |    1     |   10    |  2012-12-07 09:25:19
  11 |    1     |   6,5   |  2012-12-07 13:33:16
  12 |    3     |   6     |  2012-12-07 15:45:44
  13 |    2     |   4     |  2012-12-07 16:05:16
  14 |    2     |   34    |  2012-12-07 18:33:55
  15 |    2     |   22    |  2012-12-07 18:42:11

我想像这样显示用户分数:如果某个用户在某一天的分数超过 3 个,那么它只会得到最高的 3 个分数,为此用户每天重复该操作,然后将所有天数加在一起。我想为每个用户显示这个总和。

编辑:所以在上面的例子中,用户 1 在 06.12 上。我会将前 3 个分数加在一起并忽略第 4 个分数,然后从第二天开始添加到该数字的前 3 个分数,依此类推。我需要每个用户的号码。

编辑 2: 预期输出是:

  userid |   score  
--------------------
    1    |    59    //19 + 16.5 + 7 (06.12.) + 10 + 6.5 (07.12.)
    2    |    87    //22 + 0.5 (06.12.) + 34 + 22 + 8.5 (07.12.)
    3    |    18    //12 (06.12.) + 6 (07.12.)

我希望这更清楚:)

我非常感谢您的帮助,因为我被卡住了。

4

2 回答 2

2

请查看以下代码,如果您对我的评论的回答是yes:) 由于您的数据都是 2012 年和 11 月的数据,所以我花了一天时间。

询问:

select y.id, y.userid, y.score, y.datestamp 
from (select id, userid, score, datestamp 
      from scores
      group by day(datestamp)) as y    
where (select count(*) 
       from (select id, userid, score, datestamp
             from scores group by day(datestamp)) as x
       where y.score >= x.score
       and y.userid = x.userid
      ) =1 -- Top 3rd, 2nd, 1st    
order by y.score desc
;

结果:

ID  USERID  SCORE   DATESTAMP
8   2       8.5 December, 07 2012 00:00:00+0000
20  3       6   December, 08 2012 00:00:00+0000
1   1       5   December, 06 2012 00:00:00+0000

根据您后来对问题的更新。如果您需要按年/月/日为每个用户提供一些然后找到最高的,您可以简单地添加聚合函数,如sum上述查询。我在重复自己,因为您的样本数据只有一年,所以没有按年或按月分组。这就是为什么我花了一天。

select y.id, y.userid, y.score, y.datestamp 
from (select id, userid, sum(score) as score,
      datestamp 
from scores
group by userid, day(datestamp)) as y    
where (select count(*) 
from (select id, userid, sum(score) as score
      , datestamp
from scores
group by userid, day(datestamp)) as x
where y.score >= x.score
and y.userid = x.userid
) =1 -- Top 3rd, 2nd, 1st    
order by y.score desc
;

基于总和的结果:

ID  USERID  SCORE   DATESTAMP
1   1       47.5    December, 06 2012 00:00:00+0000
8   2       16      December, 07 2012 00:00:00+0000
20  3       6       December, 08 2012 00:00:00+0000

更新了新的源数据样本

西蒙,请看看我自己的样品。随着您的数据发生变化,我使用了我的数据。这是参考。我使用了ansi没有任何over partitionor的纯样式dense_rank。另请注意,我使用的数据是前 2 名而不是前 3 名。你可以相应地改变。

你猜怎么着,答案比你的第一个数据给出的第一印象简单 10 倍......

SQLFIDDLE

查询到 1:- 用户每天的前 2 名总和

SELECT userid, sum(Score), datestamp
FROM scores t1
where 2 >=
(SELECT count(*) 
 from scores t2
 where t1.score <= t2.score
 and t1.userid = t2.userid
 and day(t1.datestamp) = day(t2.datestamp)
 order by t2.score desc)
group by userid, datestamp 
;

查询 1 的结果:

USERID  SUM(SCORE)  DATESTAMP
1       70      December, 06 2012 00:00:00+0000
1       30      December, 07 2012 00:00:00+0000
2       22      December, 06 2012 00:00:00+0000
2       25      December, 07 2012 00:00:00+0000
3       30      December, 06 2012 00:00:00+0000
3       30      December, 07 2012 00:00:00+0000

最终查询:--所有两天用户的前 2 名总和

SELECT userid, sum(Score)
FROM scores t1
where 2 >=
(SELECT count(*) 
 from scores t2
 where t1.score <= t2.score
 and t1.userid = t2.userid
 and day(t1.datestamp) = day(t2.datestamp)
 order by t2.score desc)
group by userid
;

最终结果:

USERID  SUM(SCORE)
1      100
2      47
3      60

这是我使用的数据直接计算的快照。

在此处输入图像描述

于 2012-12-07T13:45:40.593 回答
0
SELECT 
    * 
FROM
    table1
LEFT JOIN 
    (SELECT * FROM table1 ORDER BY score LIMIT 3) as lr on DATE(lr.datestamp) = DATE(table1.datastamp)
GROUP BY 
    datestamp   
于 2012-12-07T13:17:59.180 回答