最近的日期得分最高

时间:2017-03-22 09:39:35

标签: mysql

我有下面的表格定义。

CREATE TABLE IF NOT EXISTS ranking (  
    user_id int(11) unsigned NOT NULL, 
    create_date date NOT NULL,  
    score double(8,2),
    PRIMARY KEY (user_id, create_date)
)

insert into ranking (user_id, create_date, score) values  
    (1, '2017-03-01', 100),  
    (1, '2017-03-02',  90),  
    (1, '2017-03-03',  80),  
    (1, '2017-03-04', 100), 
    (1, '2017-03-05',  90),  
    (2, '2017-03-01',  90),  
    (2, '2017-03-02',  80),  
    (2, '2017-03-03', 100),  
    (2, '2017-03-5', 100),  
    (3, '2017-03-01',  80),  
    (3, '2017-03-02', 100),  
    (3, '2017-03-03',  90),  
    (3, '2017-03-6', 100);

select * from ranking;  
    user_id | create_date | score   
          1 | 2017-03-01  |   100  
          1 | 2017-03-02  |    90   
          1 | 2017-03-03  |    80  
          1 | 2017-03-04  |   100   
          1 | 2017-03-05  |    90  
          2 | 2017-03-01  |    90   
          2 | 2017-03-02  |    80   
          2 | 2017-03-03  |   100   
          2 | 2017-03-05  |   100   
          3 | 2017-03-01  |    80   
          3 | 2017-03-02  |   100   
          3 | 2017-03-03  |    90   
          3 | 2017-03-06  |   100  
对于每个user_id,

我想要的是,获取分数最高的最新create_date。例如,在上面的示例中,对于user_id = 1,当create_date = 2017-03-01和create_date = 2017-03-04时,最高分数为100,但我只想要最近的日期和最高分,即,create_date = 2017-03-04和score = 100.查询结果如下:

user_id | create_date | score   
      1 | 2017-03-04  |   100   
      2 | 2017-03-05  |   100  
      3 | 2017-03-06  |   100  

以下是我的解决方案,它会返回预期的结果,但我相信存在更好的解决方案。

SELECT a.* from   
(  
    SELECT s1.user_id , s1.create_date, s1.score FROM ranking AS s1   
    INNER JOIN  
    (SELECT user_id , FORMAT(max(score), 0) as best_score FROM ranking GROUP BY user_id ) AS s2  
    ON s1.user_id = s2.user_id AND s1.score = s2.best_score  
) a   
NATURAL LEFT JOIN   
(  
    SELECT s1.user_id , s1.create_date, s1.score FROM ranking AS s1   
    INNER JOIN   
    (  
        SELECT user_id , create_date, score FROM ranking  
    ) s2  
    WHERE s1.user_id = s2.user_id AND s1.score = s2.score AND s1.create_date < s2.create_date  
) b  
WHERE b.user_id IS NULL;  

有人可以提供更好的解决方案吗?感谢。

3 个答案:

答案 0 :(得分:2)

SELECT t1.user_id,
       MAX(t1.create_date) AS max_date,
       t2.max_score
FROM ranking t1
INNER JOIN
(
    SELECT user_id, MAX(score) AS max_score
    FROM ranking
    GROUP BY user_id
) t2
    ON t1.user_id = t2.user_id AND
       t1.score   = t2.max_score
GROUP BY t1.user_id

<强>输出:

enter image description here

在这里演示:

Rextester

答案 1 :(得分:0)

试试这个:

select user_id, max(create_date),max(score) from ranking GROUP BY user_id

结果:

1   2017-03-04  100.00
2   2017-03-05  100.00
3   2017-03-06  100.00

select user_id, max(create_date),cast(max(score)  as UNSIGNED) as maxscore from ranking GROUP BY user_id

结果:

1   2017-03-04  100
2   2017-03-05  100
3   2017-03-06  100

答案 2 :(得分:0)

尝试此查询 -

SELECT r1.* FROM ranking r1
  JOIN (SELECT user_id, MAX(score) max_score FROM ranking GROUP BY user_id) r2
    ON r1.user_id = r2.user_id AND r1.score = r2.max_score
  JOIN (SELECT user_id, score, MAX(create_date) max_create_date FROM ranking GROUP BY user_id, score) r3
    ON r1.user_id = r3.user_id AND r1.score = r3.score AND r1.create_date = r3.max_create_date;
1   04-Mar-17   100
2   05-Mar-17   100
3   06-Mar-17   100