pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: nested query problem
Date: Thu, 6 Sep 2018 14:10:10 -0700
Message-ID: <4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com> (raw)
In-Reply-To: <CAMBRECCCSqSRZnBz2--ShfaP9fhkJYMVu+zktj6CEuZuT6Ovww@mail.gmail.com>
References: <CAMBRECCCSqSRZnBz2--ShfaP9fhkJYMVu+zktj6CEuZuT6Ovww@mail.gmail.com>

On 09/06/2018 01:59 PM, David Gauthier wrote:
> I'm having trouble with this query...
> 
> select sr.sqf_id, sr.status, fse.perl_sub_name, fse.end_datetime
>    from
>      sqf_runs sr,
>      (select perl_sub_name, end_datetime from flow_step_events_view 
> where sqf_id = sr.sqf_id order by 2 limit 1) fse
>    where sr.userid='foo';
> 
> ERROR:  invalid reference to FROM-clause entry for table "sr"
> LINE 4: ...atetime from flow_step_events_view where sqf_id = sr.sqf_id ...
>                                                               ^
> HINT:  There is an entry for table "sr", but it cannot be referenced 
> from this part of the query.

This calls for a lateral join:

     SELECT  sr.sqf_id, sr.status, fse.perl_sub_name, fse.end_datetime
     FROM    sqf_runs sr
     LEFT OUTER JOIN LATERAL (
       SELECT  perl_sub_name, end_datetime
       FROM    flow_step_events_view fsev
       WHERE   fsev.sqf_id = sr.sqf_id
       ORDER BY 2
       LIMIT 1
     ) fse
     ON      true
     WHERE   sr.userid = 'foo'
     ;

It's nearly what you had already, but `LATERAL` lets the subquery 
reference columns in the other tables.

A lateral join is conceptually a lot like running your subquery in for 
loop, looping over all the rows produced by the rest of the query. It 
doesn't have to produce 1 row for each iteration, but saying `LIMIT 1` 
ensures that here.

The `ON true` is just pro forma because you can't have a join without an 
`ON` clause.

You might prefer an INNER JOIN LATERAL, depending on your needs.

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




view thread (5+ messages)  latest in thread

Message-ID: <4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com>
Permalink:  ../4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com

 · 

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: pj@illuminatedcomputing.com
  Subject: Re: nested query problem
  In-Reply-To: <4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com>
  References: <CAMBRECCCSqSRZnBz2--ShfaP9fhkJYMVu+zktj6CEuZuT6Ovww@mail.gmail.com> <4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com>

* 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