x

SQL SERVER Query/Function to get all possible combinations

Hi,

I like to get a function which accepts an array of values and return me all possible combination.

Ex: A,B,C is the array of values passed to the function and below is the expected output.

A B C AB AC BC ABC

Even though AA, BB, CC is possible, but I don't like to return that combination because both numbers are equal.

Can some one please give me the sql query.

Thanks Sql Beginner

more ▼

asked Apr 30, 2010 at 01:43 AM in Default

avatar image

Syed
1 1 1 1

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

2 answers: sort voted first

declare @str varchar(10)

select @str = 'A,B,C'

; with
data
as
(
select , Cnt = count() over()
from dbo.fnParseList(',', @str)
),
rcte
as
(
select RowID, Data, Cnt, lvl = 1
from data

 union all            
         
 select  d.RowID, Data = r.Data + d.Data, d.Cnt, lvl = r.lvl + 1            
 from    rcte r            
     inner join data d   on  r.Data <> d.Data            
                 and d.RowID > r.RowID            
                 and r.lvl < d.Cnt            

)
select Data
from rcte
order by lvl, Data

use fnParseList from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033

more ▼

answered Apr 30, 2010 at 02:50 AM

avatar image

Squirrel 1
1.6k 1 3 5

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

Hi I have one requirment and need to do one job. Please help me

I want to a sql query to get all combinations for n strings and r pickups i.e ncr formula(ncr=n!/r!*(n-r)!) For example: RED,GREEN,YELLOW,BLUE,VOILET,BLACK,ORANGE so toal 7 colours right? but out of 7 i want choose 3 colours only. For this How many combinations will we get

more ▼

answered Jan 09 at 08:48 AM

avatar image

pcreddy1005
1

You might be better off asking this as a new question...

Jan 09 at 08:50 AM ThomasRushton ♦♦
(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:

x2020

asked: Apr 30, 2010 at 01:43 AM

Seen: 2710 times

Last Updated: Jan 09 at 08:50 AM

Copyright 2016 Redgate Software. Privacy Policy