drop table a CREATE TABLE a (Id int, Count int) insert a select 1 , 2 union all select 2 , 2 union all select 3 , 1 union all select 4 , 1 union all select 5 , 2 union all select 6 , 2 union all select 7 , 1 union all select 8 , 2 union all select 9 , 1 union all select 10 , 2declare @tb table(num varchar(50),id int,Count int) insert @tb select newid(),* from aselect * from @tb c where 5>=(select sum(count) from @tb t where c.num>=t.num)
CREATE TABLE a (Id int, Count int)
insert a
select 1 , 2
union all select 2 , 2
union all select 3 , 1
union all select 4 , 1
union all select 5 , 2
union all select 6 , 2
union all select 7 , 1
union all select 8 , 2
union all select 9 , 1
union all select 10 , 2declare @tb table(num varchar(50),id int,Count int)
insert @tb
select newid(),* from aselect * from @tb c where 5>=(select sum(count) from @tb t where c.num>=t.num)