Re: Reuse temporary calculation results in an SQL update query [SOLVDED] - Mailing list pgsql-sql

From David Johnston
Subject Re: Reuse temporary calculation results in an SQL update query [SOLVDED]
Date
Msg-id 005e01cd9f28$481c39d0$d854ad70$@yahoo.com
Whole thread Raw
In response to Re: Reuse temporary calculation results in an SQL update query [SOLVDED]  (Matthias Nagel <matthias.h.nagel@gmail.com>)
List pgsql-sql
>
> thank you. The "WITH" clause did the trick. I did not even know that such a
> thing exists. But as it turns out it makes the statement more readable and
> elegant but not faster.
>
> The reason for the latter is that both the CTE and the UPDATE statement
> have the same "FROM ... WHERE ..." part, because the tempory calculation
> needs some input values from the same table. Hence the table is looked up
> twice instead once.

This is unusual; the only WHERE clause you should require is some kind of key matching...

Like:

UPDATE tbl
SET ....
FROM (
WITH final_result AS (SELECT pkid, ....FROM tblWHERE ...
) -- /WITH
SELECT pkid, .... FROM final_result
) src  -- /FROM
WHERE src.pkid = tbl.pkid
;

If you provide an actual query better help may be provided.

David J.






pgsql-sql by date:

Previous
From: Johnny Winn
Date:
Subject: Re: Need help with a special JOIN
Next
From: Jasen Betts
Date:
Subject: Re: HOw to convert unicode to string