pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Subquery to select max(date) value
Date: Fri, 29 Mar 2019 00:50:53 +0000
Message-ID: <87mulev72k.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <alpine.LNX.2.20.1903281617010.19962@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.1902121414490.15295@salmo.appl-ecosys.com>
	<2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net>
	<alpine.LNX.2.20.1902121443290.15295@salmo.appl-ecosys.com>
	<alpine.LNX.2.20.1902121446460.15295@salmo.appl-ecosys.com>
	<8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com>
	<alpine.LNX.2.20.1902121500240.15295@salmo.appl-ecosys.com>
	<87va1oplgl.fsf@news-spur.riddles.org.uk>
	<alpine.LNX.2.20.1902151230581.31662@salmo.appl-ecosys.com>
	<878syglqvp.fsf@news-spur.riddles.org.uk>
	<alpine.LNX.2.20.1902151327540.31662@salmo.appl-ecosys.com>
	<874l94lo43.fsf@news-spur.riddles.org.uk>
	<alpine.LNX.2.20.1903281553550.19962@salmo.appl-ecosys.com>
	<CAKFQuwbQ6f9Kqqs-H+js9hi-9c1Q4pWT9pj=Kq9h3Ld2yVCCoA@mail.gmail.com>
	<alpine.LNX.2.20.1903281617010.19962@salmo.appl-ecosys.com>

>>>>> "Rich" == Rich Shepard <rshepard@appl-ecosys.com> writes:

 Rich> Tried this and did not do it correctly. Should there be two
 Rich> 'order by', one in the sub-query, the other in the top-level
 Rich> query?

Yes.

 Rich> This does not return the desired order:

 Rich> select p.person_id, p.lname, p.fname, p.direct_phone, p.active, o.org_name, sq.*
 Rich> from people as p
 Rich>      join organizations as o on p.org_id = o.org_id
 Rich>      cross join
 Rich>          lateral
 Rich>          (select a.next_contact
 Rich>          from activities as a
 Rich>              where a.person_id = p.person_id and
 Rich>              p.active='True' and
 Rich>              a.next_contact is not null
 Rich>          order by a.next_contact DESC
 Rich>          limit 1) sq
 Rich>          order by sq.next_contact DESC;

That query seems correct assuming you want the result in descending
order of next_contact. How did the actual result differ from your
expectation?

-- 
Andrew (irc:RhodiumToad)





view thread (48+ messages)  latest in thread

Message-ID: <87mulev72k.fsf@news-spur.riddles.org.uk>
Permalink:  ../87mulev72k.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87mulev72k.fsf@news-spur.riddles.org.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, rshepard@appl-ecosys.com
  Subject: Re: Subquery to select max(date) value
  In-Reply-To: <87mulev72k.fsf@news-spur.riddles.org.uk>
  References: <alpine.LNX.2.20.1902121414490.15295@salmo.appl-ecosys.com> <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> <alpine.LNX.2.20.1902121443290.15295@salmo.appl-ecosys.com> <alpine.LNX.2.20.1902121446460.15295@salmo.appl-ecosys.com> <8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com> <alpine.LNX.2.20.1902121500240.15295@salmo.appl-ecosys.com> <87va1oplgl.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1902151230581.31662@salmo.appl-ecosys.com> <878syglqvp.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1902151327540.31662@salmo.appl-ecosys.com> <874l94lo43.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1903281553550.19962@salmo.appl-ecosys.com> <CAKFQuwbQ6f9Kqqs-H+js9hi-9c1Q4pWT9pj=Kq9h3Ld2yVCCoA@mail.gmail.com> <alpine.LNX.2.20.1903281617010.19962@salmo.appl-ecosys.com> <87mulev72k.fsf@news-spur.riddles.org.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox