multiple identical calc and function in single query - Mailing list pgsql-general

From Sim Zacks
Subject multiple identical calc and function in single query
Date
Msg-id 4AE96902.3000700@compulab.co.il
Whole thread Raw
Responses Re: multiple identical calc and function in single query
List pgsql-general
I have a query which includes the same calculation and function call
multiple times.

Is the calculation and function call evaluated one time or as many times
as it is in the query?

Example:

326/getdisplayconversionmultiplebypn(pnid) is in this query 6 times. How
many times is it evaluated?


select pnid, (326/getdisplayconversionmultiplebypn(pnid)) + case when
coalesce(mpq,0)=0 then 0 else
mpq-((326/getdisplayconversionmultiplebypn(pnid)) % mpq)end as qty,
        case when not translatempq then
(326/getdisplayconversionmultiplebypn(pnid)) + case when
coalesce(mpq,0)=0 then 0 else
mpq-((326/getdisplayconversionmultiplebypn(pnid)) % mpq)end
        else ((326/getdisplayconversionmultiplebypn(pnid)) + case when
coalesce(mpq,0)=0 then 0 else
mpq-((326/getdisplayconversionmultiplebypn(pnid)) % mpq)end)/mpq  end as
mpqqty
        from manufacturerpartpn a
        inner join leadstatemfgparts b on a.partid=b.partid and
a.manufacturerid=b.manufacturerid
        where compatibilitygradeid in (50,100,200) and lifecycleid not
in (-400,-500)
        and a.parentid is null and a.PartID=1108 and leadstateid = any
('{1,3}')
        and leadstateid <> case when lfbcount>0 then 4 else -1 end
        and leadstateid <> case when lpbcount>0 then 2 else -1 end


pgsql-general by date:

Previous
From: Alban Hertroys
Date:
Subject: Re: Slow running query with views...how to increase efficiency? with index?
Next
From: William Temperley
Date:
Subject: Re: Emal reg expression