Re: Referencing an expression

From: Reece Hart <reece(at)harts(dot)net>
To: "Dispensa, Brad" <DispensaB(at)anesthesia(dot)ucsf(dot)edu>
Cc: sfpug(at)postgresql(dot)org
Subject: Re: Referencing an expression
Date: 2008-08-19 18:00:03
Message-ID: 1219168803.6495.30.camel@snafu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: sfpug

On Tue, 2008-08-19 at 09:36 -0700, Dispensa, Brad wrote:

> My question is, in an access query you can define a query expression
> and then reference that expression again later in the query:
> Example
> Select 1_quantity, 1_cost,
> [1_quantity]*[cost] as 1_subtotal,
> 2_quantity, 2_cost,
> [2_quantity]*[cost] as 2_subtotal,
> [1_subtotal]+[ 2_subtotal] as total
> From table

I think your options are

1) inline
select q1,c1,q1*c1 as e1, q2,c2,q2*c2 as e2, q1*c1+q2*c2 as total from
items

2) subquery
select ec.*,e1+e2 as total from (select q1,c1,q1*c1 as e1, q2,c2,q2*c2
as e2 from items) ec;

3) view
create view extended_costs as select q1,c1,q1*c1 as e1, q2,c2,q2*c2 as
e2 from items;
select *,e1+e2 as total from extended_costs;

If postgresql optimizes common subexpressions like q1*c1, the
performance difference between inline and subquery will be negligible
(and probably even if it doesn't). I'm fairly certain that there's no
performance difference between the subquery and view.

-Reece

--
Reece Hart, http://harts.net/reece/, GPG:0x25EC91A0

In response to

Browse sfpug by date

  From Date Subject
Next Message Josh Berkus 2008-09-05 16:56:24 PG VPS hosting in the Bay Area?
Previous Message David Fetter 2008-08-19 17:21:20 Re: Referencing an expression