0

I trying to create this update statement that adds the average runtimes from today into a temporary table. I keep getting the following syntax error:

ERROR: ERROR: syntax error at or near "INNER" Position: 22

Query = UPDATE temptbl1 AS T INNER JOIN ( select jobno, avg(elaptime) as avgrun from cmr_runinf where to_timestamp(timestmp, 'YYYYMMDDHH24MISS') > (now() - interval '1 DAY') GROUP BY JOBNO ) AS source ON T.jobno = source.jobno SET T.todayrun = source.avgrun

My statement:

UPDATE temptbl1 AS T
INNER JOIN
(
select jobno, avg(elaptime) as avgrun
from cmr_runinf
where to_timestamp(timestmp, 'YYYYMMDDHH24MISS') > (now() - interval '1 DAY')
GROUP BY JOBNO
) AS source
ON T.jobno = source.jobno
SET T.todayrun = source.avgrun

3 Answers 3

1

The SET clause should come after the UPDATE clause. Also you probably want a FROM clause in there somewhere.

UPDATE temptbl1 AS T1 
SET T1.todayrun = source.avgrun 
FROM ( 
    SELECT jobno, avg(elaptime) as avgrun 
      FROM cmr_runinf 
     WHERE to_timestamp(timestmp, 'YYYYMMDDHH24MISS') 
           > (now() - interval '1 DAY')
    GROUP BY jobno
) AS source
WHERE T1.jobno = source.jobno

Also see http://www.postgresql.org/docs/current/interactive/sql-update.html

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, I changed my query around. but now I am getting a different error... ERROR: ERROR: missing FROM-clause entry in subquery for table "source" Position: 203 Query = UPDATE temptbl1 AS T1 SET T1.todayrun = source.avgrun FROM ( select jobno, avg(elaptime) as avgrun from cmr_runinf where to_timestamp(timestmp, 'YYYYMMDDHH24MISS') > (now() - interval '1 DAY') GROUP BY SOURCE.JOBNO ) AS source Where T1.jobno = source.jobno
') AS source' needs to come before 'GROUP BY SOURCE.JOBNO
Why do you link to such an old version of the Postgres manual? I would recommend to use "current" instead of the version number in the URL
0
UPDATE temptbl1 AS T1 
SET T1.todayrun = source.avgrun 
FROM ( select jobno
            , avg(elaptime) as avgrun 
       from cmr_runinf 
       where to_timestamp(timestmp, 'YYYYMMDDHH24MISS')
                > (now() - interval '1 DAY')
       GROUP BY jobno     <-- not source.jobno, there is no source inside parenthesis
     ) AS source
WHERE T1.jobno = source.jobno

Comments

0

The way you have it, it's trying to perform the update on a virtual table (join of two physical tables), which isn't permitted.

The SET needs to come after the update

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.