Here’s an example:
--create a sample dataset create table #temp (MyRecord integer identity(1000, 7), [Name] nvarchar(50)); declare @x integer = 1;while @x <= 500 begin insert into #temp ([Name]) values ('Sample ' + convert(nvarchar(40), getdate(), 121)); set @x = @x + 1; end; -- divide into groups declare @Groups decimal(20, 4) = 30;-- how many groups we want. The overflow will go into another group select [Testing Group] = convert(integer, round( row_number() over (order by t.MyRecord) / (count(t.MyRecord) over () / (@Groups)) -- this is going to be the size of each group + 1, 0, 1 -- arguments for the round function. This will cause the decimal to be truncated ) ), t.* from #temp t;
There are probably more elegant solutions, and the code looks a little messy, but it works.
No comments:
Post a Comment