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 1oisfe-0002H9-Mo for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 07:30:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oisfd-0007bU-9s for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 07:30:21 +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 1oisfc-0007bF-Vh for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 07:30:20 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oisfZ-0006qF-BH for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 07:30:20 +0000 Received: by mail-pf1-x434.google.com with SMTP id y8so1171670pfp.13 for ; Thu, 13 Oct 2022 00:30:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=rilwp3rn85h7a9HrBVXKirRqfEYM3HRvX82L/C732uE=; b=cEYFNk6zkMwZNZHi52qJ0aCB4GcYLqLLOCyJEbwiVWUQRoU39Au/7Izjelm87Oaenw ie1IOav3mzW/DZSICYw36NMQX5EMNvPz9CrCVAT0q2Euhu3AKBwLgEhot/uSaY0YOlWO CnzgCzrLpMMUPq5aqh2xwZ4JD9ELiTFARoAQmTfSP/eYa7xEE98TiFnOu4FNZlkHGJGB qXbev8fPKW5LM2UhLIoNUmF86ac0TreMROMBdMlUJ4WXAvrKBAEnm7odc99xk+yoTPPZ Qomj+0AR4qk/SYs1dOGuNEUCQbcqvDL0FFCQmA7YbnRfm1qHQFIKfR/KW7Hjo7BdjL9T NcTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding: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=rilwp3rn85h7a9HrBVXKirRqfEYM3HRvX82L/C732uE=; b=TMZBaqD911cEIaZkZ7b23UfGpn0Z1Z3QBoKLLKwLQfwsrJSBAmw+bytu6pPUdh3/ql 8jTWrMCWL0khC3nOUaD9eTKU3ccCbruQADH1bDfPmm32INNKE7XihZdRdYJsBaWeyUCP /jFf4NKf2saCT6k1PeUOv0+9yz63BvagVHn6qPfhnstEUBjllDbbu/9QY0554ewMsI0h HoWKFqcgiFQ6aP/qftdSRjjK2Km2g3NqRl2G2DXABvlZLYWg0ELhyU9MBTj7Kb92CZq4 as3q9b/D34MjGCOYruvNHTKapeQ8yBuiEINjk32/9EN0GZrLgOI53EG7tjq71/Fd5vMX 7qyg== X-Gm-Message-State: ACrzQf3QMJrXE5tPutiUU8bJcn2ed74D2YMZhMR6C1HNVHzic7uw1hUm SFDzT2alEH1oqjE1SafFDGo= X-Google-Smtp-Source: AMsMyM7wusqyF/9b7bJSfBwBJKZss/pMgHBLaHcaJIa2yPsjA8DsggQsLrJXzHhwCiJG8dsZI/Ko0A== X-Received: by 2002:a63:d709:0:b0:461:be56:507e with SMTP id d9-20020a63d709000000b00461be56507emr19219002pgg.597.1665646215453; Thu, 13 Oct 2022 00:30:15 -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 nn13-20020a17090b38cd00b0020b21019086sm6066404pjb.3.2022.10.13.00.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 00:30:15 -0700 (PDT) Date: Thu, 13 Oct 2022 15:30:11 +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=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Thu, Oct 13, 2022 at 07:05:48AM +0000, Eagna wrote: > > relatively simple one would have thought! I tried to convert this into a Postgres query as follows: > > SELECT  o.order_id, >   o.total_price - COALESCE(sub.paid, 0) > FROM _order o > LEFT JOIN LATERAL ( >     SELECT SUM(p.amount) AS paid >     FROM payment p >     WHERE p.order_id = o.order_id > ) AS sub > WHERE o.total_price > ISNULL(sub.paid, 0);  -- << line 10 - Error occurs! > > but I receive the error: > > 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: SELECT o.order_id, o.total_price - COALESCE(sub.paid, 0) FROM _order o LEFT JOIN LATERAL ( SELECT SUM(p.amount) AS paid FROM payment p WHERE p.order_id = o.order_id ) AS sub ON true WHERE o.total_price > coalesce(sub.paid, 0);