Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0qfI-0001KS-C0 for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 15:37:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0qfG-0007Pt-9B for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 15:37:34 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0qfF-0007Pj-Ts for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 15:37:34 +0000 Received: from mail-pl0-x22a.google.com ([2607:f8b0:400e:c01::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0qfC-0000sv-KQ for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 15:37:32 +0000 Received: by mail-pl0-x22a.google.com with SMTP id m22-v6so14324232pls.5 for ; Tue, 27 Mar 2018 08:37:30 -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=EOE85DRJyrzFw+Te4a+IH6iyXb9l50SBTkqt7m18O2M=; b=p1gijUXJCfYE69W/kHsHz11YojTJTPtkFGXe8S6gfF8sG/xT1ueImGLKLImthVLiS9 0ZMZwb/M+2IoXWP+JkYf+uyO76uaZwud5L/ZGoremVFeLNkMQnei0xV6hjXSG+XEa0Sb Ec5Zu11X/OAC4tSWSQjzM35Gsf8c37I8AMKgfwkgQV/z4It5vQVauXOBB7/xXG6yjj68 YkB9b+C6nwZDZ8x4lShpiRMxRMT9FajmhG15zd+C3heaBXbEknun9rHVQu/Ahv/9xUMW I2bnNNGyZxmIP885jXICZlAnG7VsLwU34OtaMv7ilVcTqrViqmYO32FcZHjNJVOk2p0U ebRA== 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=EOE85DRJyrzFw+Te4a+IH6iyXb9l50SBTkqt7m18O2M=; b=JO5FlJngnNMunBnW9nNRqxApK19PRx7uws6Oni/Hxv1A6AWVg1HAlOHGb5PEuU8z7Y z/O5CTfnPy9qWXWUssMiYDEx1cbeoIBYWryghK4NcrXZ7z8rHzOC1XgY0NpfWA7djAri PnUvEXTwGGvXBYgH600mO64jLPT0s5V4AtLvd1kZwzxTRpV3P8ufwilqfEPOGnrBYEwS PeNNMekOBSJrNsA0Hi8zTMbqfr4mRFtNhv5bPhCZ0vpaPYZ5Y8wt6yz8O6YQ2PZSxk+n SgxyXAcn1JKAIT0TVvEvnBrJDDIT6ry+xPMXW4LKKbCM1zqohcDsSDtblH5qUg0Pcwrr g2vQ== X-Gm-Message-State: AElRT7FvaF3dH2JMzi/kEm1a8kEGEPquL8qG4XEuMqlwVoI443xEq31C MEVsZ7ROUiGK7T+SonE4wEqLyAb3 X-Google-Smtp-Source: AG47ELsIj+GjMmidq+EdBLJTr6imU5LiIuoHEd6EBwZW7w5vAannkfn35tNSixWxJ1wpHM0h9fbRBg== X-Received: by 2002:a17:902:b288:: with SMTP id u8-v6mr44894075plr.339.1522165048541; Tue, 27 Mar 2018 08:37:28 -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 k2sm3062601pgo.85.2018.03.27.08.37.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 08:37:27 -0700 (PDT) Subject: Re: Using Lateral To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: Date: Tue, 27 Mar 2018 08:37:26 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/27/2018 03:22 AM, Johann Spies wrote: > In the past I could use this in a query: > > SELECT > DISTINCT ut, > CASE > WHEN xpath_exists ('//t:address_spec/t:country/text()', > q.address_spec, > p.ns) > THEN unnest (xpath ('//t:address_spec/t:country/text()', > q.address_spec, > p.ns))::citext > ELSE NULL > END country, > > No longer. The error message suggests I should use a lateral query. > But I could not figure out in the documentation how to get the same > result using a "lateral" construct. > > Just selecting "unnest(...)" gives the wrong result because if the > xpath does not exist all the other parts of the query (like 'ut' in > this case) also are empty. It is hard to suggest something without seeing your whole query (e.g. how are you joining q & p?). But it sounds like you basically want a left join to the unnested xpath result. It could be a lateral join or not. It is common to use UNNEST with an implicit lateral join, like this: SELECT ... FROM q, UNNEST(xpath('...', q.address_spec)) But that gives you an inner join. To get an outer join you need to be more explicit. Something like this: SELECT ... FROM q, p LEFT OUTER JOIN LATERAL ( SELECT * FROM unnest(xpath('//t:address_spec/t:country/text()', q.address_spec, p.ns))::citext ) x(country) ON true (Presumably you would do something more restrictive to connect q & p though.) Yours, -- Paul ~{:-) pj@illuminatedcomputing.com