"storing" a calculated value in plsql function ?

Started by stanover 6 years ago3 messagesgeneral
Jump to latest
#1stan
stanb@panix.com

I have created a function (PLSQL) that does a complex select with joins on various
tables and views, and returns a table.

In the resultant table, I have raw data, and adjusted data. The adjusted data i
all adjusted by a common factor, which is calculated in the select. Presently, I
calculate this same adjustment factor several times in the select.

Is there a way to reference this value, multiple times, once it is calculated? Or
would I have to create a 2nd select that calculates this adjustment factor, and
stores it in a PLSQL variable< and if I do that, can I reference this stored value
in the select?

--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin

#2Luca Ferrari
fluca1978@gmail.com
In reply to: stan (#1)
Re: "storing" a calculated value in plsql function ?

On Fri, Aug 30, 2019 at 12:48 PM stan <stanb@panix.com> wrote:

In the resultant table, I have raw data, and adjusted data. The adjusted data i
all adjusted by a common factor, which is calculated in the select. Presently, I
calculate this same adjustment factor several times in the select.

Is it possible to add the computed column as output of your query?
Even define a rowtype that includes such column?

Is there a way to reference this value, multiple times, once it is calculated? Or
would I have to create a 2nd select that calculates this adjustment factor, and
stores it in a PLSQL variable< and if I do that, can I reference this stored value
in the select?

Yes, you can references variables as values on queries.
As an example <https://github.com/fluca1978/PostgreSQL-11-Quick-Start-Guide/blob/master/Chapter03/Chapter03_Listing09.sql&gt;.

Luca

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: stan (#1)
Re: "storing" a calculated value in plsql function ?

pá 30. 8. 2019 v 12:48 odesílatel stan <stanb@panix.com> napsal:

I have created a function (PLSQL) that does a complex select with joins on
various
tables and views, and returns a table.

In the resultant table, I have raw data, and adjusted data. The adjusted
data i
all adjusted by a common factor, which is calculated in the select.
Presently, I
calculate this same adjustment factor several times in the select.

Is there a way to reference this value, multiple times, once it is
calculated? Or
would I have to create a 2nd select that calculates this adjustment
factor, and
stores it in a PLSQL variable< and if I do that, can I reference this
stored value
in the select?

you can use custom configuration values like global variables

https://wiki.postgresql.org/wiki/Variable_Design#PostgreSQL_User-Defined_GUCS

Regards

Pavel Stehule

Show quoted text

--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin