Re: Reuse of Subselects

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Holger Marzen <holger(at)marzen(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Reuse of Subselects
Date: 2004-02-17 12:22:19
Message-ID: 20040217122219.GB32685@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Depend in the exact query, you can do:

SELECT column1, x.c, column1-x.c
FROM table1, (... complicated subselect ...) as x;

The above may not work if they're correlated, so you can try:

SELECT column1, column2, column1-column2
FROM
(SELECT column1, (... complicated subselect ...) as column2
FROM table1);

Hope this helps,

On Tue, Feb 17, 2004 at 01:02:43PM +0100, Holger Marzen 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?
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org

--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> If the Catholic church can survive the printing press, science fiction
> will certainly weather the advent of bookwarez.
> http://craphound.com/ebooksneitherenorbooks.txt - Cory Doctorow

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Wei Wang 2004-02-17 12:37:45 Re: to_char problem
Previous Message Holger Marzen 2004-02-17 12:02:43 Reuse of Subselects