我有一个名为Info of this schema的表:
int objectId; int time; int x, y;
系统中有很多冗余数据 - 也就是说,objectId
不是唯一的.每个objectId
可以有多个条目time, x, y
.
我想检索每个对象的最新位置列表.我开始使用此查询:
SELECT * FROM Info GROUP BY objectId
这让我只是那种我想要的清单.但是我想要获得每个Object的最新时间,所以我试过:
SELECT * FROM Info GROUP BY objectId ORDER BY time DESC
这给了我一个下载的信息time
列表.但是,我不认为它做了我想要的 - 这是time, x, y
每个对象的最新信息.
任何人都可以想象一个查询来做我想要的吗?
更新我已经尝试了前三个解决方案,以了解它们在大约50,000个Infos的数据集上如何相互作用.结果如下:
-- NO INDEX: forever -- INDEX: 7.67 s SELECT a.* FROM Info AS a LEFT OUTER JOIN Info AS b ON (a.objectId = b.objectId AND a.time < b.time) WHERE b.objectId IS NULL; -- NO INDEX: 8.05 s -- INDEX: 0.17 s select a.objectId, a.time, a.x, a.y from Info a, (select objectId, max(time) time from Info group by objectId) b where a.objectId = b.objectId and a.time = b.time; -- NO INDEX: 8.30 s -- INDEX: 0.18 s SELECT A.time, A.objectId, B.x, B.y FROM ( SELECT max(time) as time, objectId FROM Info GROUP by objectId ) as A INNER JOIN Info B ON A.objectId = b.objectId AND A.time = b.time;
一定程度上,它似乎where
表现优异inner join
.
SELECT A.time, A.objectID, B.X, B.Y FROM ( SELECT max(time) as time, objectID FROM table GROUP by objectID ) as A INNER JOIN table B ON A.objectID = b.objectID AND A.Time = b.Time
如果x&y在时间线的任何一点递减,则投票者,解决方案将无效.
一种方法是使用子查询.
select distinct a.objectID, a.time, a.x, a.y from Info a, (select objectID, max(time) time from Info group by objectID) b where a.objectID = b.objectID and a.time = b.time
编辑:添加DISTINCT以防止重复行,如果一个objectId具有相同时间的多个记录.如果有必要,取决于您的数据,问题作者提到有许多重复的行.(Tomalak补充)