Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

mysql怎么查询5分钟粒度的时间轴数据?

现在设备每1分钟会上送一次采集数据存到数据库,现在想按5分钟的粒度(比如4:00 4:05 4:10)为时间轴,查询每个时间节点附近的数据库内的数据作为该时间节点的数据,怎么用一条sql实现?

  • 示例

数据库数据:

4:00:02 a
4:01:12 b
4:02:15 c
4:03:18 d
4:04:13 e
4:05:20 f
4:06:25 g
4:07:35 h
4:08:40 i
4:09:42 j
4:10:50 k
4:11:55 l

sql查出的数据:

4:00:00 a
4:05:00 f
4:10:00 j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

首先得考虑几种情况:

  1. 同一个时间点有2条数据(如4:05:01和4:05:02),那么要展示2条的结果还是最近的一条
  2. 如果在某段时间(超过5分钟)没数据,那是否要显示中断节点的结果

推荐个思路,获取分钟值除以5取余,例如:where substr(时间字段,-5,2)/5=0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...