Re: case, new column not found

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: case, new column not found
Дата
Msg-id 28252.993167371@sss.pgh.pa.us
обсуждение исходный текст
Ответ на case, new column not found  (Martín Marqués <martin@bugs.unl.edu.ar>)
Список pgsql-sql
Martín Marqués <martin@bugs.unl.edu.ar> writes:
> 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

> Why is it? encontrados should be an attribute of type INT with the count of 
> the rows found.

No it shouldn't.  Items in the select list are not attributes.  Since
the WHERE phase logically precedes evaluation of the SELECT output list,
you can hardly expect to be able to use SELECT outputs in WHERE.

You could work around this with a nested sub-SELECT, viz
select * from(select *, (CASE ...) AS encontrados FROM admin_view) subselWHERE subsel.nivel=1 AND subsel.encontrados >
0;

at a possible penalty in performance.
        regards, tom lane


В списке pgsql-sql по дате отправления:

Предыдущее
От: Stephan Szabo
Дата:
Сообщение: Re: case, new column not found
Следующее
От: "Josh Berkus"
Дата:
Сообщение: Re: case, new column not found