使用PostgreSQL中的查询结果更新列

时间:2012-12-07 17:18:53

标签: sql database postgresql sql-update postgresql-9.2

我在PostgreSQL 9.2中有下表,其中包含时间戳:

gid [PK](bigserial),timestamp_mes(没有时区的时间戳),time_diff(间隔)
1,2012-01-23 11:03:40,空的 2,2012-01-23 11:03:42,空的 3,2012-01-23 11:03:44,空白

我添加了一个间隔列(time_diff),并希望用此查询产生的时差值填充它:

SELECT timestamp_mes - lag(timestamp_mes, 1) 
over (order by timestamp_mes) as diff
from gc_entretien.trace order by timestamp_mes

我尝试了以下查询来更新time_diff列,但没有成功:

UPDATE gc_entretien.trace set time_diff = 
(SELECT trace.timestamp_mes - lag(trace.timestamp_mes, 1) 
over (order by trace.timestamp_mes) 
from gc_entretien.trace order by timestamp_mes);

这会导致错误:

  

错误:用作表达式

的子查询返回多行

如何使用时差查询产生的值更新time_diff列?

3 个答案:

答案 0 :(得分:31)

这样的事情:

with new_values as (
   SELECT gid, 
          timestamp_mes - lag(timestamp_mes, 1) over (order by timestamp_mes) as diff
   from gc_entretien.trace 
)
update gc_entretien.trace as tr
  set time_diff = nv.diff
from new_values nv
where nv.gid = tr.gid;

答案 1 :(得分:3)

您不能在UPDATE中直接使用窗口函数,因此您需要在子SELECT中使用它 - 您已经完成了。但是,您尝试在UPDATE中使用该子SELECT的方式不是有效的语法。您需要将子SELECT放在更新的FROM子句中,如Postgres文档中所述:

http://www.postgresql.org/docs/9.2/static/sql-update.html

您要执行的操作的正确语法是:

UPDATE gc_entretien.trace t
SET time_diff = subquery.diff
FROM (SELECT {{SomeUniqueId}}, 
             timestamp_mes - lag(timestamp_mes, 1) over (order by timestamp_mes) as diff
      FROM gc_entretien.trace order by timestamp_mes) AS subquery
WHERE t.{{SomeUniqueId}} = subquery.{{SomeUniqueId}}

显然,您需要在我写的{{SomeUniqueId}}

所在行的某个唯一ID的列名中替换

答案 2 :(得分:1)

实际上您收到此错误是因为您的子查询返回多个结果,

我无法理解您的查询,

我会举一个例子来解决它,

update table t1 set time_diff= select *your_operation* from table t2 where t1.id=t2.id

这里:-your_operation意味着找时差的逻辑,