'Expensive' column in result set

From: "Harvey, Allan AC" <HarveyA(at)OneSteel(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: 'Expensive' column in result set
Date: 2007-05-08 04:16:34
Message-ID: E97A5BB7699CAD48BE2711E71247116562426E@ntlmsg03.onesteel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

I have a select that involves a column in the result set that is expensive to get.
I need to use the expensive column in a subsequent calculated column.

Is there a syntax to run the expensive function once only or does it just have to be done,
blk_speed() is the expensive function.

Example of what I would like to do, does not work of course:
select
b.block,
b.p_code,
p.description,
p.blk_speed as "set",
blk_speed( b.block ) as "actual",
actual / set * 100 as "Speed %"
from block b, product p
where b.p_code = p.p_code;

This does work
select
b.block,
b.p_code,
p.description,
p.blk_speed as "set",
blk_speed( b.block ) as "actual",
blk_speed( b.block ) / ( p.blk_speed + 0.0001 ) * 100 as "Speed %"
from block b, product p
where b.p_code = p.p_code;

OR is postgres smart enough to know it only needs to get blk_speed() once.

Thanks

Allan

The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended recipient, use, disclosure or copying of this information is prohibited. If you have received this document in error, please advise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses contained in this email or any attachments.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-05-08 04:17:28 Re: Vacuuming
Previous Message Mariano Mara 2007-05-08 03:06:58 Re: Anyone know a good opensource CRM that actually installs with Posgtres?