Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Permutations (tricky one)

Author  Topic 

olly
Starting Member

5 Posts

Posted - 2014-01-27 : 12:02:45
Hi,
We're tyring to find the best way to solve the following, hope you can help please.
We have a fairly large table of users (2500+) and the users belong to 1 or more groups (at the moment the max number of groups a user is in is 32 but it changes daily).
A sample data set looks like:
Usr | Grp
==============
SmithA | GrpA
SmithA | GrpB
MouseM | GrpD
MouseM | GrpB
MouseM | GrpC

The result set we need would be:
SmithA | GrpA
SmithA | GrpB
SmithA | GrpA,GrpB
MouseM | GrpB
MouseM | GrpC
MouseM | GrpD
MouseM | GrpB,GrpC
MouseM | GrpB,GrpD
MouseM | GrpC,GrpD
MouseM | GrpB,GrpC,GrpD

Note the alphabetical order of the grps - it's not particularly imporant but if they aren't sorted alphabetical then we'd need all combinations.

Any ideas please?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-01-27 : 14:12:47
This is kind of a pain in SQL. You could adapt a bit-wise operation to do this, but I'm not sure of the performance if you get into the BIGINT arena (over 32 groups per user). Plus you'd be limited to 64 (63?) groups per user doing that.

Here is a link that talks about this technique:
http://dba.stackexchange.com/questions/29661/sql-query-for-combinations-without-repetition

Go to Top of Page
   

- Advertisement -