Re: Help with SQL staterment

Поиск
Список
Период
Сортировка
От Thom Brown
Тема Re: Help with SQL staterment
Дата
Msg-id BANLkTimFNg0SeWkGgqMe_=CeqaLpUc1ehg@mail.gmail.com
обсуждение исходный текст
Ответ на Help with SQL staterment  (Chris Campbell <ccampbell@cascadeds.com>)
Ответы Re: Help with SQL staterment  (Chris Campbell <ccampbell@cascadeds.com>)
Список pgsql-novice
On 30 June 2011 22:59, Chris Campbell <ccampbell@cascadeds.com> wrote:
> Using PostgreSQL 9.0.2 I’m trying to write a sql statement to extract
> specific rows from the following data set.  I’ve looked at the documentation
> that talks about the Max() function but the example doesn’t go far enough:
>
> The sample fields/dataset are as follows
>
> PaymentKey,  ContactKey,  PaymentDate
> 1,  100, 01/01/2011
> 2,  100, 12/30/2010
> 3,  100, 12/31/2010
> 4,  101, 01/02/2011
> 5,  101, 12/25/2010
>
> What I want returned are rows grouped by contactkey showing the
> corresponding payment key and payment date for the record that has the max
> (newest) payment date.  So I would want my result set to look like this:
>
> 1,  100, 01/01/2011
> 4,  101, 01/02/2011
>
> I would be using this query as a sub query that is (left) joined to a master
> query by ContactKey

It should look something like this:

SELECT contactkey, max(paymentdate)
FROM my_table
GROUP BY contactkey;

You'll need to adapt it for your joined query as I don't know what
your join looks like.

Regards

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

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

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

Предыдущее
От: Chris Campbell
Дата:
Сообщение: Help with SQL staterment
Следующее
От: Chris Campbell
Дата:
Сообщение: Re: Help with SQL staterment