我有这个表,我希望使用SQL查询找到每个用户的最新位置。
该表包含列,(1)电子邮件,(2)时间戳,(3)位置。
所以如果表格如下:
email timestamp location
user1@email.com 27.08.2014 kitchen
user1@email.com 28.08.2014 livingroom
user1@email.com 29.08.2014 kitchen
user2@email.com 26.08.2014 hallway
user2@email.com 27.08.2014 kitchen
我希望它输出:
email timestamp location
user1@email.com 29.08.2014 kitchen
user2@email.com 27.08.2014 kitchen
答案 0 :(得分:2)
SELECT yt.* FROM your_table yt
INNER JOIN (
SELECT
email, MAX(timestamp) max_ts
FROM
your_table
GROUP BY email
) sq ON yt.email = sq.email AND yt.timestamp = sq.max_ts