Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fy1Xf-0006DE-Kf for pgsql-general@arkaria.postgresql.org; Thu, 06 Sep 2018 21:10:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fy1Xd-0007Ij-S9 for pgsql-general@arkaria.postgresql.org; Thu, 06 Sep 2018 21:10:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fy1Xd-0007IS-EU for pgsql-general@lists.postgresql.org; Thu, 06 Sep 2018 21:10:17 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fy1Xa-0005qF-Ot for pgsql-general@lists.postgresql.org; Thu, 06 Sep 2018 21:10:16 +0000 Received: by mail-pf1-x42b.google.com with SMTP id l9-v6so5883550pff.9 for ; Thu, 06 Sep 2018 14:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=1VBkXbwUHATsTODQZOpekrA67mg2Ml6M9BK56ndw6HQ=; b=q0cBL80cUTuciGjC4+QS1/kLGyU+oWlW4RFGutd3ICImdpUdxaAWJ71Vx8iKzTtslE aLmyww1kMgTDmiIZ9SJvAbejs7D6mh9u69r9MonLBTzCK1CgCAC/08A2mNluJ9Z1uGIr Eg7qH8qwcd68eFQZ+8zhcjiFAT/LvBYy5g4TbfFyYJ0gswyAkuOaYrn1n14jGMguxCQy PChL3NNH911clks4/m49hfVFHu2KzDPSo8T6XJQBROgxyQqhLOh7Y+ycsoKZ1fTkfzDW +eRqI3mo2AujC6Vx7tlayvwo54N03H82A/jp8WWtitkMUHuxvYXiiEWii2uAVsuOcp20 NSRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=1VBkXbwUHATsTODQZOpekrA67mg2Ml6M9BK56ndw6HQ=; b=gqnvXnxAuMmrTqXAgXldEraz5XJi9WzwASBnrLVoNw4bym6bTPats11JESxaw9pLhT GY884ZA9vT/nYJH5jiI9pJMRI+stCye87lsZNP6aGOLlhqcFFy59t1i9EMLW8OtR0LHW MaqXyEUttc6ZfJ/EKjkKs0MlU4ODzgSC4rXQCAIT9ufRyvy4d0fsYWOJf/DbdDVU062/ CLsYgwd/k1HQnqNJZPs6xR0WIsjTFQxmbl04Vz9L2CHAwM25C/VRlGB1Slmeqc4ZH0vc rJpkmDfHdPu5iWK57AlHmY18QnyZGP6sXC5Z/AT4mKCPndzDZ7B1MnEr1cOx3/nrePEC BD5Q== X-Gm-Message-State: APzg51COJPLCh0F8cEPKFgarkVYov071Dhtjz+fSwD3c/R5/DO9XJBsc ShG+p6YNbrlEK7dJASoHYMcqsD6tLuo= X-Google-Smtp-Source: ANB0VdYp3nEtmiGRGjFaaNCUMhzsEhXyRxH1QVEAWEuvl3V3D4JtNWr17OCYiBIiRjCBgAm2jRdgMw== X-Received: by 2002:a62:6d02:: with SMTP id i2-v6mr5109456pfc.218.1536268212018; Thu, 06 Sep 2018 14:10:12 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id k23-v6sm6680170pgl.42.2018.09.06.14.10.10 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Sep 2018 14:10:11 -0700 (PDT) Subject: Re: nested query problem To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: <4c19a45b-8b5d-b171-a443-d86b67f81c38@illuminatedcomputing.com> Date: Thu, 6 Sep 2018 14:10:10 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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