sql server - T-SQL All combinations in vertical -


i want find possible combinations of userid-chain following subset example:

image

the result of example must produce 4 different chain of userid:

  1. 32,34,36...39
  2. 32,35,36...39
  3. 33,34,36...39
  4. 33,35,36...39

all can displayed in vertical result table (following first example)

image

in original table have data different userid/companyid need recursive solution different iteration of currentlevel users

;with cte ( select     companyid,     currentlevel,     userid @result      currentlevel = 1  union  select           rec.companyid,     rec.currentlevel,     rec.userid @result r     inner join cte rec on      rec.companyid = r.companyid     , rec.currentlevel = r.currentlevel +1  ) 

then select

select * cte 

but not found solutions that.


Comments