Technical Article

Generating permutations

,

I have to put the script here, in the description field, since the editor above, does not work properly neither in IE nor Firefox.

Anyway this script is an example how You can generate permutations with T-SQL:

 

/******************************************************************************
 *
 * Author Rafal Skotak
 * Purpose This script shows one on ways of generating permutations
 * Date 2008-01-21
 *
 *******************************************************************************/
 

with 
 res(numb)
as
(
 select 0 as numb union 
 select 1 union 
 select 2 union 
 select 3 union 
 select 4 union 
 select 5 union
 select 6
)
select 
 res1.numb, 
 res2.numb, 
 res3.numb,
 res4.numb,
 res5.numb,
 res6.numb,
 res7.numb,

cast(res1.numb as varchar(64)) + 
 cast(res2.numb as varchar(64)) + 
 cast(res3.numb as varchar(64)) + 
 cast(res4.numb as varchar(64)) +
 cast(res5.numb as varchar(64)) +
 cast(res6.numb as varchar(64)) +
 cast(res7.numb as varchar(64)) 
 as res_glued,

char(res1.numb + ascii('A')) +
 char(res2.numb + ascii('A')) +
 char(res3.numb + ascii('A')) +
 char(res4.numb + ascii('A')) +
 char(res5.numb + ascii('A')) +
 char(res6.numb + ascii('A')) +
 char(res7.numb + ascii('A'))
 as res_text

from
 res as res1 inner join
 res as res2 on
 res1.numb <> res2.numb inner join
 res as res3 on
 res3.numb <> res1.numb and
 res3.numb <> res2.numb inner join
 res as res4 on 
 res4.numb <> res1.numb and
 res4.numb <> res2.numb and
 res4.numb <> res3.numb inner join
 res as res5 on 
 res5.numb <> res1.numb and
 res5.numb <> res2.numb and
 res5.numb <> res3.numb and
 res5.numb <> res4.numb inner join
 res as res6 on 
 res6.numb <> res1.numb and
 res6.numb <> res2.numb and
 res6.numb <> res3.numb and
 res6.numb <> res4.numb and
 res6.numb <> res5.numb inner join
 res as res7 on 
 res7.numb <> res1.numb and
 res7.numb <> res2.numb and
 res7.numb <> res3.numb and
 res7.numb <> res4.numb and
 res7.numb <> res5.numb and
 res7.numb <> res6.numb
where
 res1.numb <> res2.numb

order by
 res_text

 

 

 

 

Rate

1.75 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

1.75 (4)

You rated this post out of 5. Change rating