database - 删除数据位于 json 列中的 postgresql 数据库中的连续重复项
问题描述
所以我有一个名为 postgresql 的表state_data
,其中有两列:datetime
和state
. 该state
列是 jsonb 类型,并指定给定日期时间的各种状态数据。以下是该表的示例:
datetime | state
================================================
2018-10-31 08:00:00 | {"temp":75.0,"location":1}
2018-10-31 08:01:00 | {"temp":75.0,"location":1}
2018-10-31 08:02:00 | {"temp":75.0,"location":1}
2018-10-31 08:03:00 | {"temp":75.0,"location":2}
2018-10-31 08:04:00 | {"temp":74.8,"location":1}
2018-10-31 08:05:00 | {"temp":74.8,"location":2}
2018-10-31 08:06:00 | {"temp":74.7,"location":1}
随着时间的推移,这个表会变得非常大——尤其是我增加了采样频率——我真的只想存储连续行具有不同温度的数据。所以上表将简化为,
datetime | state
================================================
2018-10-31 08:00:00 | {"temp":75.0,"location":1}
2018-10-31 08:04:00 | {"temp":74.8,"location":1}
2018-10-31 08:06:00 | {"temp":74.7,"location":1}
如果温度数据在其自己的列中,我知道如何执行此操作,但是是否有一种简单的方法来处理此操作并根据 json 列中的项目删除所有连续的重复项?
如果我想删除两个 json 项的重复项怎么办?例如,
datetime | state
================================================
2018-10-31 08:00:00 | {"temp":75.0,"location":1}
2018-10-31 08:03:00 | {"temp":75.0,"location":2}
2018-10-31 08:04:00 | {"temp":74.8,"location":1}
2018-10-31 08:05:00 | {"temp":74.8,"location":2}
2018-10-31 08:06:00 | {"temp":74.7,"location":1}
解决方案
使用窗口函数lag():
select datetime, state
from (
select datetime, state, lag(state) over (order by datetime) as prev
from state_data
) s
where state->>'temp' is distinct from prev->>'temp'
如果表有主键,您应该在删除命令中使用它。在缺少主键的情况下,您可以state
转换为 jsonb:
delete from state_data
where (datetime, state::jsonb) not in (
select datetime, state::jsonb
from (
select datetime, state, lag(state) over (order by datetime) as prev
from state_data
) s
where state->>'temp' is distinct from prev->>'temp'
)
推荐阅读
- excel - 有没有办法在考虑某些标准条件的同时将员工平均分配到大型任务列表中?
- windows - Git pull 无法更新本地引用
- excel - InnerText 为特定的跨度类返回空
- asp.net - GET请求在邮递员上工作但不在浏览器中
- node.js - Jsonwebtoken 验证导致 NGINX 超时
- python-3.x - python concurrent.futures 跳过超时进程
- r - 如何将变量的名称及其结果插入 R 中的绘图中
- node.js - AWS S3 访问被本地主机拒绝,但在部署时工作
- php - 如何从xml中获取数据?
- c++ - 为什么可以通过构造函数将临时值分配给引用?