Re: A unique pairs version of UNNEST() ?

From: Kevin Grittner <kgrittn(at)gmail(dot)com>
To: Alban Hertroys <haramrae(at)gmail(dot)com>
Cc: Wells Oliver <wells(dot)oliver(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: A unique pairs version of UNNEST() ?
Date: 2016-01-05 21:03:32
Message-ID: CACjxUsNVu0ch93VAki4EtpehnJkg7jO5V3xB1weapL3e_mRuPQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 5, 2016 at 1:59 AM, Alban Hertroys <haramrae(at)gmail(dot)com> wrote:

> with list_of_ids as (
> select unnest(list_of_ids) as id from table
> )
> select a.id, b.id
> from list_of_ids a, list_of_ids b
> where b.id > a.id;

Or, to morph this to array output (which the OP seemed to want):

test=# with list_of_ids as (
test(# select unnest('{1,2,3,4,5}'::int[]) as id
test(# )
test-# select array [a.id, b.id]
test-# from list_of_ids a, list_of_ids b
test-# where b.id > a.id;
array
-------
{1,2}
{1,3}
{1,4}
{1,5}
{2,3}
{2,4}
{2,5}
{3,4}
{3,5}
{4,5}
(10 rows)

Nothing in that not already mentioned; just putting it all
together.

The OP mentioned wanting a count, but that wasn't too clear to me;
using a window function to number the rows, changing the comparison
from > to >= while excluding self-matches should make that pretty
easy.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2016-01-05 21:09:04 Re: Code of Conduct: Is it time?
Previous Message Tom Lane 2016-01-05 20:23:15 Re: Unique constraints and indexes.