x

sql query permutations without repetition

I need a query which can be used in (or as) a function and retrieves all permutations of n values. And I need all permutations of length k where k = 1..n.

Extended sample input and result so input has 3 values instead of 2 - however, number of input values may vary from 1 to n.

Example:

Input: table with values in one column in multiple rows

 Value  (nvarchar(500))
 ------
 Ann
 John
 Mark

Output#1: table with values concatenated in one column

 Ann
 John
 Mark
 Ann,John
 Ann,Mark
 John,Mark
 Ann,John,Mark
more ▼

asked Dec 02, 2012 at 05:52 AM in Default

avatar image

Qrosh
10 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

You might want to start by having a look at am old speed phreak contest that does permutations... http://ask.sqlservercentral.com/questions/3045/the-word-permutation-sql-problem.html

more ▼

answered Dec 02, 2012 at 10:25 AM

avatar image

ThomasRushton ♦♦
41.6k 20 50 53

(comments are locked)
10|1200 characters needed characters left

Paul White's answers to this exact question. I doubt I could add anything to his explanation.

more ▼

answered Dec 03, 2012 at 12:37 PM

avatar image

Kevin Feasel
6.2k 4 7 15

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x376
x75
x2

asked: Dec 02, 2012 at 05:52 AM

Seen: 1814 times

Last Updated: Dec 03, 2012 at 12:37 PM

Copyright 2017 Redgate Software. Privacy Policy