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 1f1FtM-0003wP-Sd for pgsql-general@arkaria.postgresql.org; Wed, 28 Mar 2018 18:33:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f1FtL-0005M5-Ps for pgsql-general@arkaria.postgresql.org; Wed, 28 Mar 2018 18:33:47 +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 1f1FtL-0005Lw-CB for pgsql-general@lists.postgresql.org; Wed, 28 Mar 2018 18:33:47 +0000 Received: from mail-pf0-x232.google.com ([2607:f8b0:400e:c00::232]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f1FtI-0003xc-Jx for pgsql-general@lists.postgresql.org; Wed, 28 Mar 2018 18:33:46 +0000 Received: by mail-pf0-x232.google.com with SMTP id j2so1377197pff.10 for ; Wed, 28 Mar 2018 11:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=doB0Tm0fV0EvmwLz0NeGJXAYGJpVC0DC95MEZbaJ1eA=; b=mp3QQLNeb3eI1J8VeZnWTUYQ6aym76LgAZF9PGR890hSmbeJEWjb3B6Xg0MdNhB1AK 0qrF23OrG9JMyo1iBbpyu/AztmTMkpNnLY+uPGQJK2FI9ka1tBs8k4XBzcdsRBC/L6Tk jDRVTZM/5sug9HQmBx7y1z+VmBf2n7/LcNdFlkr3aZzDJ0qHs9/EcAU0T5CZ4ehvIgqJ WsAQagxzhywMzAagkB6dXDm3Y/1wCEQntab3KMAFSNDWj8/Nih5BAmteRQ1He4O3jcEW SxGRzcbNz+I+Sdb0/acaTnVT5kupEwuGu32mtmNZFZrvvKkKDL4S5a2tNwHGHA5o+wMb bH3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=doB0Tm0fV0EvmwLz0NeGJXAYGJpVC0DC95MEZbaJ1eA=; b=tgPXaa/00FUmk940BzULWpSL7lCeDJ3deYZb21mCAs8MyLwfT/gaDjJuQbzGfoHl+z bWqlZclmc47EbIQmm9e4yzUyNDXCk7O2DyNEicXseOcFVoJzlQ9Z7y8P7dZ/WM/Hnm4d wTUF6zLywCsRSsSEiPOtN0ybZs/gP4GERXR9q1zb9bbAUuwrXODOxLyHFhlwizV8U8Qc GVRCRtgb4ZFGUP/Cz8WjHXRUBSxT2ZEoZjWrIkf47I3RNpqtvaekwMGmW5Kd9M6mBFO6 9ToqfZHqJY7goZk7Axkr9hS1lOdH6J+mguUkhLxY/G5zQa2q1Uafh/yglFiTBOuCQ/hl BEPw== X-Gm-Message-State: AElRT7Ho3WL6DmUBItRqzqpFq3IF/LJ4nXfY4jWtdjVZBJitOUhAygev En1jKCtRqzn6WmatBTiL2NqbqMT5 X-Google-Smtp-Source: AIpwx4/poMaPCJ8BbtSOPV+eE923b5fn4Cv8a6m/RmCd/IGXZGQFJU42iHBN+9wAj3kbukAktH9raA== X-Received: by 10.99.3.216 with SMTP id 207mr3299456pgd.163.1522262021920; Wed, 28 Mar 2018 11:33:41 -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 j20sm10254303pfa.149.2018.03.28.11.33.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Mar 2018 11:33:40 -0700 (PDT) Subject: Re: Using Lateral To: Johann Spies Cc: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: <1a4ec191-6a83-ff66-5a08-5a0cf5f501a7@illuminatedcomputing.com> Date: Wed, 28 Mar 2018 11:33:39 -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/28/2018 05:22 AM, Johann Spies wrote: > Thanks Paul. > > I was hesitant to post my full query. It is a long and complicated > query. Ha ha, you aren't joking. :-) With something that long I don't think I'd want to split every xpath call into a new join. I guess you could try it and see though. Stepping back, your original query is prepared for xpath to give 0 results or 1 result, but not 2+, and the problem is that you're getting a 2+ result from the address_spec element. What is the right behavior there? To get two rows in the end result? Just use the first/last address_spec? If you want to force 0/1 results, you can keep your structure and do this: SELECT DISTINCT ut, (xpath('//t:address_spec/t:country/text()', q.address_spec, p.ns))[1]::citext AS country, (xpath('//t:address_spec/t:city/text()', q.address_spec, p.ns))[1]::citext AS city, (xpath('//t:organizations/t:organization/text()', q.address_spec, p.ns))[1]::citext AS organisation, (xpath('//t:organizations/t:organization[@pref="Y"]/text()', q.address_spec, p.ns))[1]::citext AS prefname, (xpath ('//t:suborganizations/t:suborganization/text()', q.address_spec, p.ns))[1]::citext AS suborgname, (xpath ('/t:address_spec/@addr_no', q.address_spec, p.ns))[1]::text::integer AS addr_no (Actually I see you are already doing that for addr_no. And an aside: did you intend `/t:address_spec` instead of `//t:address_spec` there?) If you would prefer to get multiple rows back, then you'll probably need a subquery to give one row per `xpath('//t:address_spec')`, so that you can keep the cities/countries/addr_nos together. Anyway, I hope that's enough to get you started on the right path! The root cause is that UNNEST is giving you two rows where it only makes sense to have one, so you need to restrict that or pull it into a context where several rows are allowed (like a join). -- Paul ~{:-) pj@illuminatedcomputing.com