Re: Subquery to select max(date) value - Mailing list pgsql-general

From Jan Kohnert
Subject Re: Subquery to select max(date) value
Date
Msg-id 36049066.FvorQsyXWJ@kohnert-n4
Whole thread Raw
In response to Subquery to select max(date) value  (Rich Shepard <rshepard@appl-ecosys.com>)
List 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





pgsql-general by date:

Previous
From: Daniel Westermann
Date:
Subject: Is the ring buffer not used for index blocks
Next
From: PegoraroF10
Date:
Subject: Re: Server goes to Recovery Mode when run a SQL