我的表是:
id home datetime player resource
---|-----|------------|--------|---------
1 | 10 | 04/03/2009 | john | 399
2 | 11 | 04/03/2009 | juliet | 244
5 | 12 | 04/03/2009 | borat | 555
3 | 10 | 03/03/2009 | john | 300
4 | 11 | 03/03/2009 | juliet | 200
6 | 12 | 03/03/2009 | borat | 500
7 | 13 | 24/12/2008 | borat | 600
8 | 13 | 01/01/2009 | borat | 700
Run Code Online (Sandbox Code Playgroud)
我需要选择每个不同的 …
表:
UserId, Value, Date.
Run Code Online (Sandbox Code Playgroud)
我想得到UserId,每个UserId的max(Date)值.也就是说,具有最新日期的每个UserId的值.有没有办法在SQL中执行此操作?(最好是Oracle)
更新:对任何含糊不清的道歉:我需要获取所有UserIds.但对于每个UserId,只有该用户具有最新日期的那一行.
我有一个简单的表评论(id INT, revision INT, comment VARCHAR(140))与这样的一些内容:
1|1|hallo1|
1|2|hallo2|
1|3|hallo3|
2|1|hallo1|
2|2|hallo2|
Run Code Online (Sandbox Code Playgroud)
我正在搜索一个SQL语句,它将返回具有最高版本的每个评论:
1|3|hallo3|
2|2|hallo2|
Run Code Online (Sandbox Code Playgroud)
我想出了这个解决方案:
select id, revision, comment
from comments
where revision = (
select max(revision)
from comments as f
where f.id = comments.id
);
Run Code Online (Sandbox Code Playgroud)
但是在大型数据集上它很慢.有没有更好的查询来实现这一目标?