34,590
社区成员
发帖
与我相关
我的任务
分享
If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([num] int)
Insert [tb]
Select number
from master..spt_values
where number between 1 and 759
and type='p'
Go
Select NTILE(6)over(order by [num]) numnew,num from [tb]
结果自己看
declare @x int,@y int,@z int
select @x=1,@y=count(1) from tbGroup
select @z=round((select count(1) from tbItem)/cast(@y as decimal(5,1)),0)
while(@x<=@y)
begin
if @x=@y
begin
select @z=(select count(1) from tbItem where groupid is null)
end
update top(@z) tbItem
set groupid=(select top 1 id
from (select id,row_number() over(order by id) 'rn' from tbGroup) t
where t.rn=@x)
where groupid is null
select @x=@x+1
end
declare @x int,@y int,@z int
select @x=1,@y=count(1) from tbGroup
select @z=round((select count(1) from tbItem)/cast(@y as decimal(5,1)),0)
while(@x<=@y)
begin
if @x=@y
begin
select @z=(select count(1) from tbItem where groupid is null)
end
update top @z tbItem
set groupid
where groupid is null
select @x=@x+1
end