pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-sql@postgresql.org
Subject: Re: Reuse temporary calculation results in an SQL update query
Date: Sat, 29 Sep 2012 12:55:39 +0200
Message-ID: <k46k39$5th$1@ger.gmane.org> (raw)
In-Reply-To: <43516431.HfO3TYfNBy@hek506>
References: <43516431.HfO3TYfNBy@hek506>

Matthias Nagel wrote on 29.09.2012 12:49:
> Hello,
>
> is there any way how one can store the result of a time-consuming calculation if this result is needed more
>than once in an SQL update query? This solution might be PostgreSQL specific and not standard SQL compliant.
>  Here is an example of what I 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 make the SQL statement faster and secondly much more cleaner and easily to understand.


Something like:

with my_calc as (
     select pk,
            time_consuming_calculation_1 as calc1,
            time_consuming_calculation_2 as calc2
     from foo
)
update foo
   set startTime = my_calc.calc1,
       stopTime = my_calc.calc2,
       duration = my_calc.calc2 - calc1
where foo.pk = my_calc.pk;

http://www.postgresql.org/docs/current/static/queries-with.html#QUERIES-WITH-MODIFYING






view thread (10+ messages)  latest in thread

Message-ID: <k46k39$5th$1@ger.gmane.org>
Permalink:  ../k46k39$5th$1@ger.gmane.org/
Also on:    postgresql.org/message-id/k46k39$5th$1@ger.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: Reuse temporary calculation results in an SQL update query
  In-Reply-To: <k46k39$5th$1@ger.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox