简体   繁体   English

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

[英]Updating a column with the results of a query in PostgreSQL

I have the following table in PostgreSQL 9.2 which contains time stamps: 我在PostgreSQL 9.2中有下表,其中包含时间戳:

gid [PK] (bigserial), timestamp_mes (timestamp without time zone), time_diff (interval) gid [PK](bigserial),timestamp_mes(没有时区的时间戳),time_diff(interval)
1, 2012-01-23 11:03:40, empty 1,2012-01-23 11:03:40,空的
2, 2012-01-23 11:03:42, empty 2,2012-01-23 11:03:42,空的
3, 2012-01-23 11:03:44, empty 3,2012-01-23 11:03:44,空的

I have added a interval column (time_diff) and would like to fill it with time difference values resulting from this query: 我添加了一个间隔列(time_diff),并希望用此查询产生的时差值填充它:

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

I have tried the following query to update the time_diff column, with no success: 我尝试了以下查询来更新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);

This results in an error: 这会导致错误:

ERROR: more than one row returned by a subquery used as an expression 错误:用作表达式的子查询返回的多行

How should I proceed to update the time_diff column with the values resulting from the time difference query? 我应该如何使用时差查询生成的值更新time_diff列?

Something like this: 像这样的东西:

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;

You can't directly use a window function in an UPDATE, so you instead need to use it in a sub-SELECT - which you have done. 您无法在UPDATE中直接使用窗口函数,因此您需要在子SELECT中使用它 - 您已经完成了。 However, the way you've tried to use that sub-SELECT in your UPDATE is not valid syntax. 但是,您尝试在UPDATE中使用该子SELECT的方式不是有效的语法。 You need to put the sub-SELECT in the FROM clause of your update, as explained by the Postgres docs here: 您需要将子SELECT放在更新的FROM子句中,如Postgres文档所述:

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

The correct syntax for what you want to do is: 您想要做的正确语法是:

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}}

Obviously, you'll need to substitute in the column name of some unique id that your rows have where I've written {{SomeUniqueId}} 显然,您需要在列的名称中替换您的行所具有的唯一ID: {{SomeUniqueId}}

Actually you are getting this error because your subquery returns multiple result, 实际上你得到这个错误,因为你的子查询返回多个结果,

I am not able to understand your query so, 我无法理解您的查询,

I will give you an example to solve it, 我会举个例子来解决它,

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

Here :-your_operation means the logic of finding time difference, 这里 :-your_operation意味着找时差的逻辑,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM