Re: Reuse temporary calculation results in an SQL update query

Поиск
Список
Период
Сортировка
От Jasen Betts
Тема Re: Reuse temporary calculation results in an SQL update query
Дата
Msg-id k46lh4$b08$1@reversiblemaps.ath.cx
обсуждение исходный текст
Ответ на Reuse temporary calculation results in an SQL update query  (Matthias Nagel <matthias.h.nagel@gmail.com>)
Список pgsql-sql
On 2012-09-29, Matthias Nagel <matthias.h.nagel@gmail.com> wrote:
> Hello,
>
> is there any way how one can store the result of a time-consuming calculation if this result is needed more than once
inan SQL update query? This solution might be PostgreSQL specific and not standard SQL compliant. Here is an example of
whatI want:
 
>
> UPDATE table1 SET
>    StartTime = 'time consuming calculation 1',
>    StopTime = 'time consuming calculation 2',
>    Duration = 'time consuming calculation 2' - 'time consuming calculation 1'
> WHERE foo;
>
> It would be nice, if I could use the "new" start and stop time to calculate the duration time. First of all it would
makethe SQL statement faster and secondly much more cleaner and easily to understand.
 
>
> Best regards, Matthias


use a CTE.
http://www.postgresql.org/docs/9.1/static/queries-with.html


with a as (  select 'time consuming calculation 1' as tcc1         , 'time consuming calculation 2' as tcc2
)
update table1
SET StartTime = a.tcc1     StopTime = a.tcc2    Duration =  a.tcc2 - a.tcc1 
WHERE foo;

you man need to move foo into the CTE too.


-- 
⚂⚃ 100% natural




В списке pgsql-sql по дате отправления:

Предыдущее
От: Jasen Betts
Дата:
Сообщение: Re: HOw to convert unicode to string
Следующее
От: Thomas Kellerer
Дата:
Сообщение: Re: Reuse temporary calculation results in an SQL update query