Pgsql在数据库中查找类似记录
我有一张桌子,上面有这个的布局 传感器识别号 时间 价值 1. 2020-12-22 09:00:00 20.5 1. 2020-12-22 10:00:00 21.5 1. 2020-12-22 11:00:00 22.5 1. 2020-12-22 12:00:00 23.5 2. 2020-12-22 09:00:00 30.5 2. 2020-12-24 10:00:00 31.5 2. 2020-12-24 11:00:00 32.5 2. 2020-12-24 12:00:00 33.5 查找传感器id 1和2具有相同日期的所有参考 为此,您可以使用聚合:Pgsql在数据库中查找类似记录,sql,postgresql,count,sql-update,subquery,Sql,Postgresql,Count,Sql Update,Subquery,我有一张桌子,上面有这个的布局 传感器识别号 时间 价值 1. 2020-12-22 09:00:00 20.5 1. 2020-12-22 10:00:00 21.5 1. 2020-12-22 11:00:00 22.5 1. 2020-12-22 12:00:00 23.5 2. 2020-12-22 09:00:00 30.5 2. 2020-12-24 10:00:00 31.5 2. 2020-12-24 11:00:00 32.5 2. 2020-12-24 12:00:00 33
select time
from readings
when sensor_id in (1, 2)
group by time
having count(*) = 2
这将为您提供两个传感器都有数据的所有时间
s
从问题的其余部分来看,我认为您希望更改记录上的传感器id(例如,从1
更改为2
),但它不会与现有记录冲突。这表明:
update readings r
set sensor_id = 2
where sensor_id = 1 and not exists (
select 1 from readings r1 where r1.sensor_id = 2 and r1.time = r.time
)