我有一个predicted_data
的postgres表:
id | lat | lon | predicted
----+-----+-----+-----------
1 | 1 | 1 | 10
2 | 2 | 2 | 20
3 | 3 | 3 | 30
我还有另一个observed_data
表:
id | lat | lon | observed | error
----+-----+-----+----------+-------
1 | 1 | 1 | 11 |
2 | 2 | 2 | 25 |
3 | 3 | 3 | 32 |
我想填充error
的{{1}}列。对于第一行,我会这样做:
observed_data
我是否可以通过某种方式编写此查询,以便同时对每一行进行查询?
我的最终结果应该是:
UPDATE observed_data
SET error =
((SELECT predicted FROM predicted_data WHERE lat = 1 AND lon = 1) - (SELECT observed FROM observed_data WHERE lat = 1 AND lon = 1))
WHERE lat = 1 AND lon = 1 AND id = 1;
答案 0 :(得分:6)
您可以对联接使用UPDATE:
update observed_data as od
set error = pd.predicted - od.observed
from predicted_data as pd
where (pd.lat, pd.long) = (od.lat, od.long);