Re: Reuse of Subselects

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Holger Marzen <holger(at)marzen(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Reuse of Subselects
Date: 2004-02-17 14:45:15
Message-ID: 20040217144515.GA8800@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Feb 17, 2004 at 13:02:43 +0100,
Holger Marzen <holger(at)marzen(dot)de> wrote:
> Hi all,
>
> if I have something like this:
>
> SELECT column1,
> (... complicated subselect ...),
> column1 - (... same subselect as above ...)
> FROM table1;
>
> do I really have to rewrite the subselect a 2nd time if I need that
> result in another column's expression?

You should be able to use a join. The syntax won't be simpler, but you
might get nearly a 2 fold speed up.

Assuming that the subselect depends on the current row, you want to do
something like:

SELECT (
SELECT column1, complicated, column1 - complicated FROM
(complicated select AS complicated) AS comptable)
) FROM table1;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-02-17 14:47:36 Re: psql, 7.4, and the \d command
Previous Message Murali Mohan 2004-02-17 14:21:37 unsubscribe