Re: Help with SQL staterment

Поиск
Список
Период
Сортировка
Искать
От
Thom Brown
Тема
Re: Help with SQL staterment
Дата
Msg-id
BANLkTin2xvC1PZebXO3XD8YNXzPtmYoE3w@mail.gmail.com
Ответ на
Список
Дерево обсуждения
Help with SQL staterment Chris Campbell <ccampbell@cascadeds.com>
Re: Help with SQL staterment Steve Crawford <scrawford@pinpointresearch.com>
Re: Help with SQL staterment Thom Brown <thom@linux.com>
Re: Help with SQL staterment Chris Campbell <ccampbell@cascadeds.com>
Re: Help with SQL staterment Thom Brown <thom@linux.com>
Re: Help with SQL staterment Steve Crawford <scrawford@pinpointresearch.com>
Re: Help with SQL staterment Thom Brown <thom@linux.com>
Re: Help with SQL staterment Chris Campbell <ccampbell@cascadeds.com>
problem "Phillip B Childs" <it@marisusa.com>
Re: problem Richard Broersma <richard.broersma@gmail.com>
On 30 June 2011 23:25, Chris Campbell  wrote:
>>It should look something like this:
>
>>SELECT contactkey, max(paymentdate)
>>FROM my_table
>>GROUP BY contactkey;
>
> Yeah, that's what I started with. The problem is that I "need" the payment key returned in addition to the other fields based on the max(paymentdate).  When I add paymentkey to the mix I keep ending up with a cartesianed product showing multiple paymentkeys.

You can try this:

SELECT DISTINCT ON (contactkey)
    paymentkey, contactkey, paymentdate
FROM
    my_table
ORDER BY
    contactkey, paymentdate DESC

-- 
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
В списке pgsql-novice по дате отправления
От: Steve Crawford
Дата:
Сообщение: Re: Help with SQL staterment
От: Steve Crawford
Дата:
Сообщение: Re: Help with SQL staterment
FAQ