From: Josh Berkus <josh@agliodbs.com>
To: Martín Marqués <martin@bugs.unl.edu.ar>
To: pgsql sql Mailing List <pgsql-sql@postgresql.org>
Subject: Re: case, new column not found
Date: Thu, 21 Jun 2001 16:53:25 -0700
Message-ID: <web-75619@davinci.ethosmedia.com> (raw)
In-Reply-To: <01062120022108.16528@bugs>
Martín,
> select *,(
> (CASE WHEN titulo LIKE '%Matematica%' THEN 1 ELSE 0 END) +
> (CASE WHEN descripcion LIKE '%Matematica%' THEN 1 ELSE 0 END) +
> (CASE WHEN incumbencia LIKE '%Matematica%' THEN 1 ELSE 0 END) )
> AS encontrados
> FROM admin_view
> WHERE admin_view.nivel=1 AND encontrados > 0;
>
> ERROR: Attribute 'encontrados' not found
The problem is that you're trying to do a WHERE filtering on a
calculated column by its alias. The query engine (correct me if I'm
wrong) wants to evaluate the WHERE clause before the SELECT columns are
returned; as a result, there is no "encontrados" to evaluate as it has
not been calculated yet.
If you really want the results above, you need to:
SELECT admin_view.*, encontrados
FROM admin_view,
(SELECT ((CASE WHEN titulo LIKE '%Matematica%' THEN 1 ELSE 0 END) +
(CASE WHEN descripcion LIKE '%Matematica%' THEN 1 ELSE 0 END) +
(CASE WHEN incumbencia LIKE '%Matematica%' THEN 1 ELSE 0 END) )
AS encontrados, id FROM admin_view) sub_admin
WHERE admin_view.nivel=1
AND admin_view.id = sub_admin.id
AND encontrados > 0;
This forces encontrados to be evaluated first because it's in a
subselect.
A simpler approach would be:
> select *,(
> (CASE WHEN titulo LIKE '%Matematica%' THEN 1 ELSE 0 END) +
> (CASE WHEN descripcion LIKE '%Matematica%' THEN 1 ELSE 0 END) +
> (CASE WHEN incumbencia LIKE '%Matematica%' THEN 1 ELSE 0 END) )
> AS encontrados
> FROM admin_view
> WHERE admin_view.nivel=1 AND
(titulo LIKE '%Matematica%' OR descripcion LIKE '%Matematica%'
OR incumbencia LIKE '%Matematica%');
Which would give you the same results.
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: josh@agliodbs.com, martin@bugs.unl.edu.ar
Subject: Re: case, new column not found
In-Reply-To: <web-75619@davinci.ethosmedia.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox