Re: Subselect AS and Where clause

From: Uwe Schroeder <uwe(at)oss4u(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Thomas Kellerer <spam_eater(at)gmx(dot)net>
Subject: Re: Subselect AS and Where clause
Date: 2011-01-26 09:00:29
Message-ID: 201101260100.29505.uwe@oss4u.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Uwe Schroeder, 26.01.2011 08:34:
> > I have a query like this:
> >
> > SELECT a,b,c, (select problem from other_table where id=a) as problem
> > FROM mytable WHERE a=1
> >
> > So far so good. Actually "problem" always resolves to one record, so it's
> > not the "multiple records returned" problem.
> >
> > What I try to do is this:
> >
> > SELECT a,b,c, (select problem from other_table where id=a) as problem
> > FROM mytable WHERE a=1 and problem = 3
> >
> > see the "problem=3" part in the where clause? The error I get is
> >
> > SQLError: (ProgrammingError) column "problem" does not exist
>
> You need to wrap the whole SELECT in order to be able to use the column
> alias:
>
> SELECT *
> FROM (
> SELECT a,
> b,
> c,
> (select problem from other_table where id=a) as problem
> FROM mytable
> ) t
> WHERE a=1
> AND problem = 3
>
> Regards
> Thomas

Thanks a lot Thomas!

there's the reason why open source like postgresql is far supperior to
anything commercial - an answer when you need it!

Works like a charm now.

Uwe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message tuanhoanganh 2011-01-26 09:01:45 Re: PostgreSQL 9.0 service error : The service did not respond to the start or control request in a timely fashion.
Previous Message tuanhoanganh 2011-01-26 08:58:04 PostgreSQL 9.0 service error : The service did not respond to the start or control request in a timely fashion.