Re: Subquery to select max(date) value

From: Jan Kohnert <nospam001-lists(at)jan-kohnert(dot)de>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Subquery to select max(date) value
Date: 2019-02-13 10:32:02
Message-ID: 36049066.FvorQsyXWJ@kohnert-n4
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am Dienstag, 12. Februar 2019, 23:23:49 CET schrieb Rich Shepard:
> The query is to return the latest next_contact date for each person. Using
> the max() aggregate function and modeling the example of lo_temp on page 13
> of the rel. 10 manual I wrote this statement:

You don't even need a subselect to do so:

select
P.person_id, P.lname, P.fname, P.direct_phone,
O.org_name, max(A.next_contact) as next_contact
from People as P
join Organizations as O
on P.org_id = O.org_id
join Activities as A
on P.person_id = A.person_id
group by
P.person_id, P.lname, P.fname,
P.direct_phone, O.org_name;

--
Kind regards Jan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message PegoraroF10 2019-02-13 11:41:23 Re: Server goes to Recovery Mode when run a SQL
Previous Message Daniel Westermann 2019-02-13 09:48:14 Is the ring buffer not used for index blocks