pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Julien Rouhaud <rjuju123@gmail.com>
To: Eagna <eagna@protonmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Problem with LATERAL
Date: Thu, 13 Oct 2022 16:34:13 +0800
Message-ID: <Y0fNhShrA0aGAXAH@jrouhaud> (raw)
In-Reply-To: <wlSYzEPIXpDDcXltpFyND7yNWiXJWBfnZaT7IU5ksn_x8p6CJiZVNFweIKE_EgKLb9asJfw5ruqKpg2jYGStLdSlxKbeo2huV8pmJRjlpAg=@protonmail.com>
References: <Ibm2WcUuz9rJSUtznoh50x1Kn7YekKSFyMijP6xIVnrEl9WfWmEbVYboiEdEp5Alpin6LlkQGnDXcsVs_R7vSR0DxhtHShOtVj8B8NhAcHo=@protonmail.com>
	<Y0e+g9CaCSic5YHD@jrouhaud>
	<wlSYzEPIXpDDcXltpFyND7yNWiXJWBfnZaT7IU5ksn_x8p6CJiZVNFweIKE_EgKLb9asJfw5ruqKpg2jYGStLdSlxKbeo2huV8pmJRjlpAg=@protonmail.com>

On Thu, Oct 13, 2022 at 08:04:03AM +0000, Eagna wrote:
>
> > > ERROR: syntax error at or near "WHERE"
> > > LINE 10: WHERE o.total_price > ISNULL(sub.paid, 0);
>
>
> > There error here is because a JOIN clause requires a join condition. Adding an
> > "ON true" is probably what you want. You would also need to change isnull()
> > with coalesce().
>
> > The final query should be:
>
> ...
> ...
> ...
> > ) AS sub ON true
> ...
> ...
>
> OK - I see that it works now - which is great!
>
> However, it's unclear to me what, exactly, it is that is "TRUE"?
>
> What am I joining to what?
>
> The syntax is unclear to me - if I'm joining, I should be joining on tab_A.field_x = tab_B.field_y - no?

Well, yes but the join condition in that case is already in the WHERE clause in
the sub select, so trying to put an actual join clause would be unnecessary and
add extra cost.

But I'm not sure why you want a LATERAL clause in the first place, wouldn't
this query have the same meaning?

SELECT  o.order_id,
  o.total_price - coalesce(sum(p.amount), 0)
FROM _order o
LEFT JOIN payment p ON p.order_id = o.order_id
GROUP BY o.order_id, o.total_price
HAVING o.total_price > coalesce(sum(p.amount), 0);

It should perform better if you have a lot of orders, as it can be executed
with something better than a nested loop.

> Why does SQL Server's OUTER APPLY not require this?

I don't know much about sql server, I'm assuming CROSS APPLY is an alias for
LEFT JOIN LATERAL () ON TRUE.





view thread (6+ messages)

Message-ID: <Y0fNhShrA0aGAXAH@jrouhaud>
Permalink:  ../Y0fNhShrA0aGAXAH@jrouhaud/
Also on:    postgresql.org/message-id/Y0fNhShrA0aGAXAH@jrouhaud

 · 

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: rjuju123@gmail.com, eagna@protonmail.com
  Subject: Re: Problem with LATERAL
  In-Reply-To: <Y0fNhShrA0aGAXAH@jrouhaud>
  References: <Ibm2WcUuz9rJSUtznoh50x1Kn7YekKSFyMijP6xIVnrEl9WfWmEbVYboiEdEp5Alpin6LlkQGnDXcsVs_R7vSR0DxhtHShOtVj8B8NhAcHo=@protonmail.com> <Y0e+g9CaCSic5YHD@jrouhaud> <wlSYzEPIXpDDcXltpFyND7yNWiXJWBfnZaT7IU5ksn_x8p6CJiZVNFweIKE_EgKLb9asJfw5ruqKpg2jYGStLdSlxKbeo2huV8pmJRjlpAg=@protonmail.com> <Y0fNhShrA0aGAXAH@jrouhaud>

* 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