Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible Unique Combinations 3

Status
Not open for further replies.

jhall01

Programmer
Jul 3, 2003
328
US
I have a table that with 1 column that I need to create a dynamic list of combinations 3 wide.

Table:

letters
-------
a
b
c
d
e

need to output:

abc,abd,abe,acd,ace,ade,bcd,bce,cde

I can only have a unique combination, so no matter the order the combination cannot be used again. This needs to be dynamic so that if i add a letter to the list the combinations get output with all the possible combinations.

Right now I am just focusing on 3-letter combinations only no matter how long the list.

Thanks in advance

 
Code:
[Blue]SELECT[/Blue] Y1.Letters[Gray]+[/Gray]Y2.Letters[Gray]+[/Gray]Y3.Letters 
   [Blue]FROM[/Blue] YourTable Y1 [Blue]INNER[/Blue] [Gray]JOIN[/Gray] YourTable Y2
      [Blue]ON[/Blue] Y1.Letters[Gray]<[/Gray]Y2.Letters
   [Blue]INNER[/Blue] [Gray]JOIN[/Gray] YourTable Y3
      [Blue]ON[/Blue] Y2.Letters[Gray]<[/Gray]Y3.Letters
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
very clever donutman - worthy of a star

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Gee, I wish the question was for 6-letter combinations, maybe I'd have gotten 6 stars! :)
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
just tested your solution on the whole alphabet for 10 possible 10 letter combinations and it returned 5311735 rows in 3 mins 48 seconds - pretty nifty

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top