我有一个日期时间的时间序列,存储在mySQL中的双列,并且想每分钟对时间序列进行一次采样(即,以一分钟的间隔拉出最后一个值)。在一个select语句中是否有一种有效的方法来做到这一点?
蛮力方式将涉及选择整个序列并在客户端进行采样,或者为每个点发送一个选择(例如select * from data where timestamp<xxxxxxxxx order by timestamp desc limit 1)。
您可以将时间戳转换为UNIX时间戳,然后按分组unix_timestamp DIV 60并从每个组中提取最大时间戳。然后,将获得的列表加入到原始表中,以获取获得的时间戳的数据。
基本上,它可能看起来像这样:
SELECT
t.* /* you might want to be more specific here */
FROM atable t
INNER JOIN (
SELECT
MAX(timestamp) AS timestamp
FROM atable
GROUP BY UNIX_TIMESTAMP(timestamp) DIV 60
) m ON t.timestamp = m.timestamp
Run Code Online (Sandbox Code Playgroud)