Sybase DBMS
在我的玩家表格中,我有4列:
如果我想找到玩家的得分,其中post_dt是最大值,而add_dt是该post_dt和那个玩家的最大值,我该怎么做?我的下面的查询需要很长时间才能运行,而且效率可能不是很高。
select player, score from tb t1
where post_dt in
(select max(add_dt)
from tb t2
where t1.player = t2.player and t2.post_dt in
(select max(post_dt)
from tb t2
where t1.player = t2.player))
表
PLAYER SCORE POST_DT ADD_DT
001 15 2017-02-01 2017-01-15
001 26 2017-02-01 2017-01-17
001 31 2017-01-28 2017-01-10
002 4 2017-03-25 2017-02-25
002 14 2017-03-25 2017-02-13
002 27 2017-03-25 2017-03-05
003 31 2017-01-02 2016-12-25
003 4 2017-01-03 2016-12-25
查询的预期结果:
01, 26, 2017-02-01, 2017-01-17
02, 27, 2017-03-25, 2017-03-05
03, 4, 2017-01-03, 2016-12-25
答案 0 :(得分:1)
您可以通过使用相关子查询来实现。
SELECT t1.player,
t1.score
FROM tb t1
WHERE t1.post_dt = (SELECT max(t2.post_dt)
FROM tb t2
WHERE t2.player = t1.player)
AND t1.add_dt = (SELECT max(t2.add_dt)
FROM tb t2
WHERE t2.player = t1.player
AND t2.post_dt = t1.post_dt);
或者,如果您的DBMS支持窗口功能,则使用rank()
窗口功能。
SELECT x.player,
x.score
FROM (SELECT t.player,
t.score,
rank() OVER (PARTITION BY t.player
ORDER BY t.post_dt DESC,
t.add_dt DESC) r
FROM tb t) x
WHERE x.r = 1;
另一个选择是内部联接子查询,它们分别通过player
或player
和post_dt
来构建最大值。
SELECT t1.player,
t1.score
FROM tb t1
INNER JOIN (SELECT t2.player,
max(t2.post_dt) post_dt
FROM tb t2
GROUP BY t2.player) t3
ON t3.player = t1.player
AND t3.post_dt = t1.post_dt
INNER JOIN (SELECT t3.player,
t3.post_dt,
max(t3.add_dt) add_dt
FROM tb t3
GROUP BY t3.player,
t3.post_dt) t4
ON t4.player = t1.player
AND t4.post_dt = t1.post_dt
AND t4.add_dt = t1.add_dt;