Re: Aggregate in Correlated SubQuery

Поиск
Список
Период
Сортировка
От Andrew - Supernews
Тема Re: Aggregate in Correlated SubQuery
Дата
Msg-id slrnej3org.27so.andrew+nonews@atlantis.supernews.net
обсуждение исходный текст
Список pgsql-general
On 2006-10-15, Niederland <niederland@gmail.com> wrote:
> Before postgresql 8.1.5, I could do the following to find the first
> lead that created a prospect in my application.
>
> SELECT
>   Lead.LeadID,
>   Prospect.ProspectID
> FROM
>   Prospect INNER JOIN Lead USING (ProspectID)
> WHERE
>   Lead.CreationDate = (SELECT MIN(Lead.CreationDate) FROM Lead AS LL
> WHERE LL.ProspectID = Lead.ProspectID)

I think that was always wrong, and that what you wanted was
MIN(LL.CreationDate).

The aggregate in the subquery must be an aggregate over the subquery's
rows, not over the outer query.

(Though there are ways to do this query without the subquery at all)

--
Andrew, Supernews
http://www.supernews.com - individual and corporate NNTP services

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

Предыдущее
От: Ron Johnson
Дата:
Сообщение: Re: A query planner that learns
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Re: BEGIN WORK READ ONLY;