Thanks Johnathan.
In general, the issue is related to a binomial coefficient theory. The return rows should be nCm, read as n choose m. Here is the list of values:
m n nCm
------- --------- -------
2 2 1
3 2 3
4 2 6
5 2 10
6 2 15
7 2 21
Using you script can’t get correct returns. For example if a table has 6 rows and two pairs of dups, then return rows should be 15 as
col
----
a
b
c
d
d
a
(6 row(s) affected)
----
ab
ac
bc
ad
bd
cd
ad
bd
cd
dd
aa
ab
ac
ad
ad
(15 row(s) affected)
However, your script only returns 11 rows as follows
----
ab
ac
bc
ad
bd
cd
ad
bd
cd
dd
aa
(11 row(s) affected)