HAVING and column alias

Поиск
Список
Период
Сортировка
От Mike Mascari
Тема HAVING and column alias
Дата
Msg-id 3E566A02.1030204@mascari.com
обсуждение исходный текст
Ответы Re: HAVING and column alias  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
I have the following query which gets generated dynamically from
a user-interface, particularly the subselect in the target list.
I cannot rewrite the subselect in the target list as a normal
join expression, since additional target list expressions may be
specified by the user. The below is just an example:

SELECT
SUM(p.qty),
(SELECT date_trunc('day', sales.active)
  FROM sales
  WHERE sales.purchase = p.purchase) AS field1
FROM purchases p
GROUP BY field1;

This works fine, unless the subselect in the target list doesn't
match any rows, in which case NULL is the result of the
subselect. I would like to further qualify the query with a
HAVING clause, but PostgreSQL doesn't like column aliases nor
ordinal values in the HAVING clause:

SELECT
SUM(p.qty),
(SELECT date_trunc('day', sales.active)
  FROM sales
  WHERE sales.purchase = p.purchase) AS field1
FROM purchases p
GROUP BY field1
HAVING (field1 IS NOT NULL);

ERROR: Attribute 'field1' not found

This is PostgreSQL 7.2.1. Any tips?

Mike Mascari
mascarm@mascari.com
















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

Предыдущее
От: Dennis Gearon
Дата:
Сообщение: transactions
Следующее
От: "Cristian Custodio"
Дата:
Сообщение: Mutating table (urgent)