user461316
user461316

Reputation: 913

SQL column permutation

Let's say I have a table with with one column made up of 4 rows.

Names

name1

name2

name3

name4

How could I get all permutations of this column rows. ie

name1 name2 name3 name4

name1 name2 name4 name3

ETC.

Upvotes: 0

Views: 1415

Answers (2)

Bill Karwin
Bill Karwin

Reputation: 562731

select t1.name, t2.name, t3.name, t4.name 
from mytable t1
join mytable t2 on t2.name not in (t1.name)
join mytable t3 on t3.name not in (t1.name, t2.name)
join mytable t4 on t4.name not in (t1.name, t2.name, t3.name)

Upvotes: 1

Beth
Beth

Reputation: 9617

join it to itself?

select t1.name, t2.name, t3.name, t4.name
from table t1, table t2, table t3, table t4

Upvotes: 1

Related Questions