Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oitfc-0006ga-T2 for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 08:34:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oitfb-0007Uz-Q0 for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 08:34:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oitfb-0007Un-Dv for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 08:34:23 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oitfW-0007MM-Qp for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 08:34:23 +0000 Received: by mail-pj1-x1033.google.com with SMTP id h12so1388976pjk.0 for ; Thu, 13 Oct 2022 01:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ID3UL7oGx9V+Qu6r1eeB1poM5O26AOCTKWn/ht0y7cI=; b=lPabQGxej7ofIq34SU5ZOoR/27pAtjXE51wlR69W7IHIjn+gh91NwvOds4TIRW6bBY ezQNoPoFmGwKeA1kRUWfur2X7GhBdUtEpsseWrK14NTq4xdgoJQJfsStxSoqzhFioLnM C3FMdWyzi0/gEKfcDpRnn9pT7iH6Pgl4Zf04aipyF4JQDskMIN8C3Q2XFM3J2RdvkT86 WmCiCASmzZE19n86y1eXCokXHjOwel4MGesAKYMUECoxN+5I3SzMcI9lJKrwqWDN4yRE PrcQ+K/xDFeQm90td3h04ORdWW6rPwD6a6zfu8WAM37NGGjB87t32CkXPjnBMlUt1R6S NpBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ID3UL7oGx9V+Qu6r1eeB1poM5O26AOCTKWn/ht0y7cI=; b=tuVH6uScJTkSEGCimpWR0crvHVDr+V71+Xa85YuYIHu+ZUJB3nQ67Gcej8+3Bs3sf/ tikEsB+H7K3MUfYEPc3QtDFn+8FiLXWkaOI8z2fLOfQn49/iHBil77jc7XRa/LWgrrDp g9OWHw3hSk0wVER/j/aT6QzhRSa3X8/a1wvyAC0r0a6FOUKSFQ5a6yOwkYI7R+A/8Zm0 2KEzW6EWwvs5bLEDG1NVIvpNp+B4O1H2m8QOHyb0HNTe/nloFlmkJLIkAXcWc8vDX56s R0ZxmrUW78UVIwc43jT+rn+t/zGpf6ecUmQrbmNYTfPWkIJqWRMita3bjdSeQXujswfg HG2Q== X-Gm-Message-State: ACrzQf1K1UE5tVrR8OaRnILlku3/q4tyeEFF8g61Q6BGbgdJO+F/ZEB3 DtfAHaRkfK0qxe3z4GIaf+Q= X-Google-Smtp-Source: AMsMyM6XxUzH482asBxlVRFgc/6sQlIojjAggKfXsD5h12Jiy1Y91vtQ1KPOLWyscowzfGmsub5buA== X-Received: by 2002:a17:902:f78c:b0:17a:ef1:e902 with SMTP id q12-20020a170902f78c00b0017a0ef1e902mr34018029pln.5.1665650056718; Thu, 13 Oct 2022 01:34:16 -0700 (PDT) Received: from jrouhaud (2001-b011-1006-dfce-1544-b7ba-3ed8-f7a2.dynamic-ip6.hinet.net. [2001:b011:1006:dfce:1544:b7ba:3ed8:f7a2]) by smtp.gmail.com with ESMTPSA id c2-20020a170902d48200b00182305a787fsm7987506plg.62.2022.10.13.01.34.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 01:34:16 -0700 (PDT) Date: Thu, 13 Oct 2022 16:34:13 +0800 From: Julien Rouhaud To: Eagna Cc: "pgsql-general@lists.postgresql.org" Subject: Re: Problem with LATERAL Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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.