Re: error with Subquery

From: Ragnar <gnari(at)hive(dot)is>
To: Max Bondaruk <maxim(dot)bondaruk(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: error with Subquery
Date: 2006-12-08 11:52:41
Message-ID: 1165578761.379.95.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On fös, 2006-12-08 at 13:58 +0300, Max Bondaruk wrote:
> Hi!
>
> error with Subquery alias...

> SELECT *,(SELECT COUNT(id)
> FROM articles a WHERE a.lft < articles.lft AND a.rgt > articles.rgt) AS depth
> FROM articles
> where (depth < 3)
> ORDER BY lft

you cannot refer to depth in the where because it is not
an attribute of the table in the FROM list.

it may be more obvious if we replace the subquery with a constant:
SELECT *, 999 as depth
FROM articles
WHERE (depth < 3)

however you should be able to do
SELECT * FROM
( SELECT *,
(SELECT COUNT(id) FROM articles a
WHERE a.lft < articles.lft
AND a.rgt > articles.rgt
) AS depth
FROM articles
) AS foo
WHERE (depth < 3)
ORDER BY lft

gnari

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Luca Ferrari 2006-12-08 11:53:12 help understanding analyze
Previous Message Andrew Sullivan 2006-12-08 11:42:17 Re: porting time calcs to PG