declare @categories TABLE ( ID nvarchar(max),
CatCode nvarchar(max)
)
insert into @categories
values ('01', 'A'), ('02', 'B'), ('03', 'C'), ('04', 'D'), ('05', 'E'), ('06', 'F'), ('07', 'G'), ('08', 'H'), ('09', 'I');
-- ('10', 'J'), ('11', 'K'), ('12', 'L'), ('13', 'M'), ('14', 'N'), ('15', 'O'), ('16', 'P'), ('17', 'Q'), ('18', 'R');
Declare @counter int
Select @counter = COUNT(*)
from @categories;
With Permutations (permutation, IDs, Depth)
AS (
Select c.CatCode,
c.ID + ';',
Depth = 1
From @categories c
union all
Select permutation + c.CatCode,
IDs + ID + ';',
Depth = Depth + 1
from @categories as c,
Permutations as p
Where p.Depth < @counter
AND IDs not like '%' + ID + ';%'
)
Select *
from Permutations
where Depth = @counter
order by permutation
순열 sql 인데요.. 중복되지 않게 9개(I) 중에 3개씩 묶는 조합으로 할려면 어떻게 바꿔야 할까요?
초보라 막막하네요.. 고수님들 한수 부탁드립니다...
|