sql - 平均时间戳数据在 postgres 中给出错误
问题描述
一段时间以来,我一直在努力寻找答案。让我尝试迭代问题。我有一个名为 myTable 的表:(注意:下表只是具有数千行的大表的一小部分)
timestamp_original data
2020/05/24 15.30.31.620 0
2020/05/24 15.30.31.649 1
2020/05/24 15.30.31.658 2
2020/05/24 15.30.31.668 3
2020/05/24 15.30.31.688 4
2020/05/24 15.30.31.698 5
2020/05/24 15.30.31.708 6
2020/05/24 15.30.31.738 7
2020/05/24 15.30.31.748 8
2020/05/24 15.30.31.758 9
2020/05/24 15.30.31.768 10
2020/05/24 15.30.31.809 11
2020/05/24 15.30.31.810 12
2020/05/24 15.30.31.812 13
2020/05/24 15.30.31.838 14
2020/05/24 15.30.31.848 15
我的期望是获得每毫秒之间的数据平均值,同时将毫秒舍入到 10 秒。
timestamp_new myData
2020/05/24 15.30.31.6 2.5
2020/05/24 15.30.31.7 8
2020/05/24 15.30.31.8 13
我的代码如下:
SELECT date_trunc('milliseconds','timestamp_original')::timestamp(1) as timestamp_new,
avg(data) as myData
FROM myTable
GROUP BY 1 ORDER BY timestamp_new;
但我得到的输出如下,这没有任何意义
timestamp_new myData
2020/05/24 15.30.31.6 0.5
2020/05/24 15.30.31.7 5
2020/05/24 15.30.31.8 12
请帮我。
解决方案
转换为timestamp(1)
舍入而不是截断。一个简单的解决方案是减去 50 毫秒:
SELECT date_trunc('millisecond', timestamp_original - interval '50 millisecond')::timestamp(1) as timestamp_new,
avg(data) as myData
FROM myTable
GROUP BY 1
ORDER BY timestamp_new;
这是一个 db<>fiddle。
推荐阅读
- python - 仅将包含某个单词的 Excel sheet_names 读入 pandas 数据框
- angular - 如何在 NativeScript 的安全存储插件中删除数据存储在特定对象中?[NativeScript 角度]
- javascript - 引导选项卡将所有选项卡保持为“活动”并在第一次选择后停止工作
- mongodb - 如何通过 mongoexport 将参数传递给视图?
- java - 如何在单独的行上输出字符串数组,每行允许 N 个字符
- javascript - 在 WORDPRESS 中使用简码重新加载 Div
- python - 在 Keras 中,使用 SGD,为什么 model.fit() 训练顺利,但逐步训练方法给出了爆炸梯度和损失
- javascript - Mapbox GL JS:在 Studio 中更新图层上的多边形填充颜色
- couchbase - Couchbase - 获取所有文档前缀
- php - 使用 orWhere 与 eloquent