检索查询中的最新记录
Posted
技术标签:
【中文标题】检索查询中的最新记录【英文标题】:Retrieving the most recent records within a query 【发布时间】:2010-07-05 22:17:19 【问题描述】:我有以下表格: tblPerson:
PersonID | Name
---------------------
1 | John Smith
2 | Jane Doe
3 | David Hoshi
tbl位置:
LocationID | Timestamp | PersonID | X | Y | Z | More Columns...
---------------------------------------------------------------
40 | Jan. 1st | 3 | 0 | 0 | 0 | More Info...
41 | Jan. 2nd | 1 | 1 | 1 | 0 | More Info...
42 | Jan. 2nd | 3 | 2 | 2 | 2 | More Info...
43 | Jan. 3rd | 3 | 4 | 4 | 4 | More Info...
44 | Jan. 5th | 2 | 0 | 0 | 0 | More Info...
我可以生成一个 SQL 查询来获取每个人的位置记录,如下所示:
SELECT LocationID, Timestamp, Name, X, Y, Z
FROM tblLocation
JOIN tblPerson
ON tblLocation.PersonID = tblPerson.PersonID;
生成以下内容:
LocationID | Timestamp | Name | X | Y | Z |
--------------------------------------------------
40 | Jan. 1st | David Hoshi | 0 | 0 | 0 |
41 | Jan. 2nd | John Smith | 1 | 1 | 0 |
42 | Jan. 2nd | David Hoshi | 2 | 2 | 2 |
43 | Jan. 3rd | David Hoshi | 4 | 4 | 4 |
44 | Jan. 5th | Jane Doe | 0 | 0 | 0 |
我的问题是我们只关心最近的位置记录。因此,我们只对以下行真正感兴趣:LocationID 41、43 和 44。
问题是:我们如何查询这些表以提供我们每个人的最新数据?需要进行哪些特殊分组才能产生所需的结果?
【问题讨论】:
你能把最终的解决方案贴出来吗? 【参考方案1】:mysql 没有排名/分析/窗口功能。
SELECT tl.locationid, tl.timestamp, tp.name, X, Y, Z
FROM tblPerson tp
JOIN tblLocation tl ON tl.personid = tp.personid
JOIN (SELECT t.personid,
MAX(t.timestamp) AS max_date
FROM tblLocation t
GROUP BY t.personid) x ON x.personid = tl.personid
AND x.max_date = tl.timestamp
SQL Server 2005+ 和 Oracle 9i+ 支持分析,因此您可以使用:
SELECT x.locationid, x.timestamp, x.name, x.X, x.Y, x.Z
FROM (SELECT tl.locationid, tl.timestamp, tp.name, X, Y, Z,
ROW_NUMBER() OVER (PARTITION BY tp.name ORDER BY tl.timestamp DESC) AS rank
FROM tblPerson tp
JOIN tblLocation tl ON tl.personid = tp.personid) x
WHERE x.rank = 1
使用变量来获得与 MySQL 上的 ROW_NUMBER 相同的功能:
SELECT x.locationid, x.timestamp, x.name, x.X, x.Y, x.Z
FROM (SELECT tl.locationid, tl.timestamp, tp.name, X, Y, Z,
CASE
WHEN @name != t.name THEN
@rownum := 1
ELSE @rownum := @rownum + 1
END AS rank,
@name := tp.name
FROM tblLocation tl
JOIN tblPerson tp ON tp.personid = tl.personid
JOIN (SELECT @rownum := NULL, @name := '') r
ORDER BY tp.name, tl.timestamp DESC) x
WHERE x.rank = 1
【讨论】:
感谢 OMG。我使用了 SQL Server 2005 并对其进行了一些更改以产生所需的结果。【参考方案2】:正如@Mark Byers 所提到的,这个问题经常出现在 Stack Overflow 上。
根据您的表格,这是我最常推荐的解决方案:
SELECT p.*, l1.*
FROM tblPerson p
JOIN tblLocation l1 ON p.PersonID = l1.PersonID
LEFT OUTER JOIN tblLocation l2 ON p.PersonID = l2.PersonID AND
(l1.timestamp < l2.timestamp OR l1.timestamp = l2.timestamp AND l1.LocationId < l2.LocationId)
WHERE l2.LocationID IS NULL;
要查看其他示例,请关注我在您的问题中添加的标签 greatest-n-per-group
。
【讨论】:
感谢 Bill 为我添加该标签!【参考方案3】:这是一个经典的“每组最大”问题,几乎每天都会出现在 Stack Overflow 上。有很多方法可以解决它,您可以通过searching Stack Overflow 找到示例解决方案。这是您可以在 MySQL 中执行此操作的一种方法:
SELECT
location.LocationId,
location.Timestamp,
person.Name,
location.X,
location.Y,
location.Z
FROM (
SELECT
LocationID,
@rn := CASE WHEN @prev_PersonID = PersonID
THEN @rn + 1
ELSE 1
END AS rn,
@prev_PersonID := PersonID
FROM (SELECT @prev_PersonID := NULL) vars, tblLocation
ORDER BY PersonID, Timestamp DESC
) T1
JOIN tblLocation location ON location.LocationID = T1.LocationId
JOIN tblPerson person ON person.PersonID = location.PersonID
WHERE rn = 1
【讨论】:
以上是关于检索查询中的最新记录的主要内容,如果未能解决你的问题,请参考以下文章