Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvh5L-0005Fx-HE for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 11:59:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvh5K-0000Rv-An for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 11:59:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvh5J-0000Rn-U3 for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 11:59:58 +0000 Received: from mout.kundenserver.de ([212.227.126.134]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hvh5H-0000jP-6q for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 11:59:56 +0000 Received: from [10.8.0.6] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.129]) with ESMTPSA (Nemesis) id 1MysiI-1iISn71OlO-00vwcJ for ; Thu, 08 Aug 2019 13:59:51 +0200 Subject: Re: lead() with arrays - strange behaviour To: pgsql-general@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net> Date: Thu, 8 Aug 2019 13:59:50 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:CW0kdBhUaPQGjQZhE5v55axT1oY3t34wD1ij7/gVUUbhbi4IKAc 6wd0pNMTKi8V0kQmLsUk/3fMPiFzDAgN79RbBUhBF5hwLRTkta51AYSjpAHq6wyk1unDM3Z T0ZcqCpqa2hU7WvjKkGKB2kz3HiMybHeKjshd1TwmeByxLlgjIOyjZ9V3Yt676F1JM4J0Qj MEH3SFWA7jgbI1lYuGigw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:M8bN69J4QIs=:wsI2DhainHm/oetJLko+jW MfkI3nWoMiwDDt7rYAtTCw/z5oZC684FlUULlnegyN4tOSuPL0C6BI0B+6gkfxk1IFU/62ZsE fG1ZrKNykgEaKQQkt5pIQUGUM4ZdsuOcMICA2LApIUd123W7mVJYHGq3IFfh6AzDPnwc/1Rni ntP+xs56Zs7/dhHV3x6+NnFbVNK8TBruX17qFW6N6LLCBU9ftXsMwZZ0KyIFvwGfnTeKnEfte lBECvEI2Y+XKGfuc4ZRAItxFcZgsudjxqR9aZBSV/T5VD1izPdehm5/ugbr0vm0OpZdRvKR9j 6vVVLUgKtKBxSeH8yKSIy74z+3C1/ZJHLoLtj9abRC0I3VX5TP0jK79ohdMG4JffcyRR5c12J EVc6kx0UZ7BB+QfB2hivQ/Lbauv84flBdo2eIRLtfaDw602RWYPu1o+t1McjUkqEP6c7Ke/t8 Yv0TI/ci/XX7ud/hTDWOFfU4uIkJPnvNON3HdUpRKO/o4TOSos5nQgpYr8QYupfHz61zG3hSM ArU8PFUjpQViWyfY1R9DT//86IKKTyjPLXiuiCSu3MlG++c9mOmytCJA908iO89k4ME+FmyZK u8SCNnryzTyc2P2Ue27M3m57jyCI9++9OXhXZeueyF3JCp7kl4Y2/xKlWVYELv5WfL9c90ggm ET2uy5ADLLKHXB3psUrhtkNAqkMeW8eMc1AshB85+lIZK7UOnMMIJ4Gf+MbNDA3VhDwnR7hj2 d6kYNcZyWHnWKu8UxMLIrd++U+6VHBRHo+QnI0XP2TsUBpvtlZ9pz2wYqJY= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Rowley schrieb am 08.08.2019 um 13:03: >> The following statement tries to find the overlapping values in id_list between the current row and the next row: >> >> select id, >> id_list, >> lead(id_list) over (order by id) as next_list, >> array(select unnest(id_list) intersect select unnest(lead(id_list) over (order by id))) as common_ids >> from sample_data; >> >> The above returns: >> >> id | id_list | next_list | common_ids >> ---+---------+-----------+----------- >> 1 | {1,2,3} | {2,3,4} | {} >> 2 | {2,3,4} | {4,5,6} | {} >> 3 | {4,5,6} | | {} >> >> The empty array for "common_ids" is obviously incorrect. > > I think you're confused with what the SELECT with the empty FROM > clause does here. In your subquery "id_list" is just a parameter from > the outer query. LEAD(id_list) OVER (ORDER BY id) is never going to > return anything since those are both just effectively scalar values, > to which there is no "next" value. id_list is a column in the table and as you can see in the output lead(id_list) most definitely returns the array from the next row. and "select unnest(some_array)" works just fine as you can see when "next_list" is taken from the derived table. Thomas