Re: lead() with arrays - strange behaviour - Mailing list pgsql-general

From David Rowley
Subject Re: lead() with arrays - strange behaviour
Date
Msg-id CAKJS1f9hLjqQ+q6wEkU3CvuzR7GrvrDopOW6rTpceD9gTfxcUw@mail.gmail.com
Whole thread Raw
In response to lead() with arrays - strange behaviour  (Thomas Kellerer <spam_eater@gmx.net>)
Responses Re: lead() with arrays - strange behaviour  (Thomas Kellerer <spam_eater@gmx.net>)
List pgsql-general
On Thu, 8 Aug 2019 at 21:06, Thomas Kellerer <spam_eater@gmx.net> wrote:
> 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.

--
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services



pgsql-general by date:

Previous
From: Francisco Olarte
Date:
Subject: Re: Having the issue in while selecting the data and feltering inorder by.
Next
From: Thomas Kellerer
Date:
Subject: Re: lead() with arrays - strange behaviour