Re: PL/PGSQL for permutations?

From: Joe Conway <mail(at)joeconway(dot)com>
To: stimits(at)comcast(dot)net
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: PL/PGSQL for permutations?
Date: 2003-10-07 23:37:47
Message-ID: 3F834E4B.1030100@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

D. Stimits wrote:
> table field pair. E.G., if I had in table 'one':
> left right
> ==== =====
> a b
> a c
> b d
>
> ...then I'd need a list of a, b, c, d, and produce a new table:
> left right
> ==== =====
> a b
> a c
> a d
> b a
> b c
> b d
> c a
> c b
> c d
> d a
> d b
> d c

I don't have 7.2 to test on (and you really should upgrade to 7.3.4 if
possible anyway), but why not:

create table t1(f1 text, f2 text);
insert into t1 values('a','b');
insert into t1 values('a','c');
insert into t1 values('b','d ');

select a, b
from
(select distinct f1 as a from t1 union select distinct f2 from t1)
as ss1,
(select distinct f1 as b from t1 union select distinct f2 from t1)
as ss2
where ss1.a != ss2.b;
a | b
----+----
a | b
a | c
a | d
b | a
b | c
b | d
c | a
c | b
c | d
d | a
d | b
d | c
(12 rows)

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Browne 2003-10-08 01:20:55 Re: "select count(*) from contacts" is too slow!
Previous Message D. Stimits 2003-10-07 23:25:37 PL/PGSQL for permutations?