pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
nested query problem
5+ messages / 3 participants
[nested] [flat]

* nested query problem
@ 2018-09-06 20:59 David Gauthier <davegauthierpg@gmail.com>
  2018-09-06 21:10 ` Re: nested query problem Paul Jungwirth <pj@illuminatedcomputing.com>
  2018-09-06 21:11 ` Re: nested query problem Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 5+ messages in thread

From: David Gauthier @ 2018-09-06 20:59 UTC (permalink / raw)
  To: pgsql-general

Hi:

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.


If this is indeed a foul, how can I accomplish the same thing ?

Thanks in Advance !

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: nested query problem
  2018-09-06 20:59 nested query problem David Gauthier <davegauthierpg@gmail.com>
@ 2018-09-06 21:10 ` Paul Jungwirth <pj@illuminatedcomputing.com>
  2018-09-06 21:15   ` Re: nested query problem David Gauthier <davegauthierpg@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Paul Jungwirth @ 2018-09-06 21:10 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: nested query problem
  2018-09-06 20:59 nested query problem David Gauthier <davegauthierpg@gmail.com>
  2018-09-06 21:10 ` Re: nested query problem Paul Jungwirth <pj@illuminatedcomputing.com>
@ 2018-09-06 21:15   ` David Gauthier <davegauthierpg@gmail.com>
  2018-09-06 21:19     ` Re: nested query problem David Gauthier <davegauthierpg@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: David Gauthier @ 2018-09-06 21:15 UTC (permalink / raw)
  To: pj@illuminatedcomputing.com; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Not quite.  This returns one value. In the actual "sqf_runs" table, there
are many records with user_id = 'foo'.  I want one line for each where the
fse.p-erl_sub_name and fse.end_datetime values are the latest values found
in the flow_step_events_view view where the sqf_ids match.

On Thu, Sep 6, 2018 at 5:10 PM Paul Jungwirth <pj@illuminatedcomputing.com>
wrote:

> 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
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: nested query problem
  2018-09-06 20:59 nested query problem David Gauthier <davegauthierpg@gmail.com>
  2018-09-06 21:10 ` Re: nested query problem Paul Jungwirth <pj@illuminatedcomputing.com>
  2018-09-06 21:15   ` Re: nested query problem David Gauthier <davegauthierpg@gmail.com>
@ 2018-09-06 21:19     ` David Gauthier <davegauthierpg@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David Gauthier @ 2018-09-06 21:19 UTC (permalink / raw)
  To: pj@illuminatedcomputing.com; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Wow, I take that back.  I thought there were many recs with "foo" but there
wa sonly one.
When I ran this against a value that actually had multiple records, it ran
fine.

Sorry for that.
And Thanks for this query !


On Thu, Sep 6, 2018 at 5:15 PM David Gauthier <davegauthierpg@gmail.com>
wrote:

> Not quite.  This returns one value. In the actual "sqf_runs" table, there
> are many records with user_id = 'foo'.  I want one line for each where the
> fse.p-erl_sub_name and fse.end_datetime values are the latest values found
> in the flow_step_events_view view where the sqf_ids match.
>
> On Thu, Sep 6, 2018 at 5:10 PM Paul Jungwirth <pj@illuminatedcomputing.com>
> wrote:
>
>> 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
>>
>>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: nested query problem
  2018-09-06 20:59 nested query problem David Gauthier <davegauthierpg@gmail.com>
@ 2018-09-06 21:11 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2018-09-06 21:11 UTC (permalink / raw)
  To: David Gauthier <davegauthierpg@gmail.com>; +Cc: pgsql-general

David Gauthier <davegauthierpg@gmail.com> writes:
> 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.

If you actually meant to access the outer "sqf_runs" RTE from the
sub-query, you need to mark it LATERAL:

select sr.sqf_id, sr.status, fse.perl_sub_name, fse.end_datetime
  from
    sqf_runs sr,
    lateral (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';

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-09-06 21:19 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-09-06 20:59 nested query problem David Gauthier <davegauthierpg@gmail.com>
2018-09-06 21:10 ` Paul Jungwirth <pj@illuminatedcomputing.com>
2018-09-06 21:15   ` David Gauthier <davegauthierpg@gmail.com>
2018-09-06 21:19     ` David Gauthier <davegauthierpg@gmail.com>
2018-09-06 21:11 ` Tom Lane <tgl@sss.pgh.pa.us>

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