简体   繁体   English

获取最近30分钟的数据并获取最新的行

[英]Get data from last 30 minutes and get the latest rows

I have a table with AIS marine data that gets updated very often. 我有一张AIS海洋数据表,经常更新。

What I need is the data from the last 30 minutes and from that result the newest rows and MMSI should be unique. 我需要的是过去30分钟的数据,从那个结果来看,最新的行和MMSI应该是唯一的。

The query I have now: 我现在的查询:

select max(timestamp) as timestamp, mmsi, navstatus, rot, sog, lon, lat, cog,
thead, man, mtype from ais_cnb
where (timestamp > (now() - interval 30 minute))
group by mmsi
order by timestamp desc

It seems like all the data except the timestamp is old. 似乎除时间戳之外的所有数据都是旧的。

If you are wanting the latest row from the last 30 minutes for each unique "mmsi" that has one, then using a join to a subquery where you find the max timestamps first should work, like: 如果你想要每个唯一的“mmsi”有一个最后30分钟的最新行,那么使用连接到子查询,你可以找到最大的时间戳首先应该工作,如:

SELECT timestamp, a.mmsi, navstatus, rot, sog, lon, lat, cog, thead, man, mtype
FROM ais_cnb a INNER JOIN
(SELECT mmsi, MAX(timestamp) AS max_timestamp FROM ais_cnb
 WHERE timestamp > (now() - interval 30 minute)
 GROUP BY mmsi) t
ON ((timestamp = t.max_timestamp) AND (a.mmsi = t.mmsi))

Well, there is a syntactic error there. 好吧,那里有句法错误。 As I explained here some time ago, you can't reference a calculated field in a where clause, so the timestamp you're getting is actually the field, not the aggregated function (the max() ). 正如我解释这里前一段时间,你不能引用在计算领域where条款,所以你得到的时间戳实际上是领域,而不是聚合函数( max() You didn't realised that because you named it the same way as the field. 你没有意识到这一点,因为你的命名方式与字段相同。 Try running this and you'll see: 试试这个,你会看到:

select max(timestamp) as timestamp2, mmsi, navstatus, rot, sog, lon, lat, cog,
thead, man, mtype from ais_cnb
where (timestamp2 > (now() - interval 30 minute))
group by mmsi
order by timestamp desc

Now, regardless of whether you're properly selecting those records or not, if you first get all the latest 30 minutes data, and then you get just the newest data from that subset... isn't it the same as getting the newest data? 现在,无论您是否正确选择这些记录,如果您首先获得所有最新的30分钟数据,然后您只获得该子集中的最新数据...是否与获取最新数据相同数据?

Additionally it might be a good idea to add all the other fields to the group by. 此外,将所有其他字段添加到组中可能是个好主意。

Or maybe I'm getting something wrong. 或许我得错了。 Can you please elaborate on that? 你能详细说明吗?

Edit: To filter the grouped data you need to add a HAVING clause. 编辑:要过滤分组数据,您需要添加HAVING子句。 Your previous query should be written the following way (but I'm not sure if that is what you're looking for): 您之前的查询应该按以下方式编写(但我不确定这是否是您要查找的内容):

select max(timestamp) as timestamp2, mmsi, navstatus, rot, sog, lon, lat, cog,
thead, man, mtype from ais_cnb
group by mmsi
having (timestamp2 > (now() - interval 30 minute))
order by timestamp desc

I do think, however, that you might be looking for the greatest max of each group, which should be solved with a different query... But as I said, I don't have enough info about the problem to conclude that. 但是,我确实认为你可能正在寻找每个组的最大最大值,这应该通过不同的查询来解决......但正如我所说,我没有足够的信息来得出结论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM