Re: Help with a JOIN.

Поиск
Список
Период
Сортировка
Искать
От
Kevin Grittner
Тема
Re: Help with a JOIN.
Дата
Msg-id
1406145912.24536.YahooMailNeo@web122302.mail.ne1.yahoo.com
Ответ на
Re: Help with a JOIN. (Paul Linehan)
Список
Дерево обсуждения
Help with a JOIN. Paul Linehan <linehanp@tcd.ie>
Re: Help with a JOIN. Ken Benson <Ken@infowerks.com>
Re: Help with a JOIN. Paul Linehan <linehanp@tcd.ie>
Re: Help with a JOIN. David G Johnston <david.g.johnston@gmail.com>
Re: Help with a JOIN. Paul Linehan <linehanp@tcd.ie>
Re: Help with a JOIN. Paul Linehan <linehanp@tcd.ie>
Re: Help with a JOIN. Kevin Grittner <kgrittn@ymail.com>
Re: Help with a JOIN. Ken Benson <Ken@infowerks.com>
Re: Help with a JOIN. Mike Swierczek <mike.swierczek@gmail.com>
Paul Linehan  wrote:

> I have two tables - ticket and comment (DDL and DML below) and I
> want to get the latest comment on a ticket which hasn't been
> modified for some arbitrary period (in this example, 2 years (60M
> seconds)).

A simple description like that, coupled with the DDL and data,
makes life really easy for someone to help.  :-)  Try this:

SELECT DISTINCT ON (t.ticket_id)
    t.ticket_id,
    t.ticket_description,
    c.comments_timestamp AS c_max_date,
    c.comments_comment
  FROM ticket AS t
  JOIN comment c ON c.ticket_id = t.ticket_id
  WHERE c.comments_timestamp < extract(epoch FROM TIMESTAMP '2014-07-20') - 60000000
  ORDER BY
    t.ticket_id,
    c.comments_timestamp DESC;

SELECT DISTINCT ON gives you one row for each distinct combination
of values in parentheses, and when there are duplicates it keeps
the first one based on the ORDER BY clause.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

В списке pgsql-novice по дате отправления
От: David G Johnston
Дата:
От: Shreesha
Дата:
Сообщение: WAL for backup
FAQ