22,301
社区成员




-- 测试数据
declare @t table (id int,oid int, detailid int, sku varchar(20),name varchar(50),qty int)
insert into @t(id,oid,detailid,sku,name,qty)
select 3175902,2211450,3879244,'AU21012760','Trilogy趣乐活 天然原装有机野玫瑰果精油 45ml 0.1235',10
union all
select 3175903,2211450,3879242,'AU21012138','Freezeframe 祛皱纹眼袋黑眼圈精华眼霜 15ml 0.1',2
union all
select 3175904,2211450,3879241,'AU21012105','Pawpaw 天然木瓜膏 25克 0.1', 10
union all
select 3175905,2211450,3879243,'AU21012341','Swisse 高含量维生素C泡腾片 60片 0.455',2
union all
select 3175906,2211450,3879245,'AUEN1560','Eaoron水光洁面Hyaluronic Cleanser 100ml 0.18',1
union all
select 3175907,2211450,3879246,'AUGMW001','G_M 经典白盖日用绵羊油 250g 0.39', 3
union all
select 3175908,2211450,3879247,'AUPA1004','Pure-Ora皮尔奥亚洲参人参胶囊 (30粒) 0.15',1
-- 数据处理
set nocount on
declare @t2 table(groupcode int,id int,qty int)
declare @id int,@qty int,@groupcode int
declare ap scroll cursor for
select id,qty
from @t
where oid=2211450
order by newid()
open ap
fetch first from ap into @id,@qty
select @groupcode=1
while(@@fetch_status<>-1)
begin
if (isnull((select sum(qty) from @t2 where groupcode=@groupcode),0)+@qty>16)
begin
select @groupcode=@groupcode+1
end
insert into @t2(groupcode,id,qty) select @groupcode,@id,@qty
fetch next from ap into @id,@qty
end
close ap
deallocate ap
set nocount off
-- 结果
select b.groupcode,a.*
from @t a
inner join @t2 b on a.id=b.id
order by b.groupcode
/*
groupcode id oid detailid sku name qty
----------- ----------- ----------- ----------- -------------------- -------------------------------------------------- -----------
1 3175904 2211450 3879241 AU21012105 Pawpaw 天然木瓜膏 25克 0.1 10
1 3175907 2211450 3879246 AUGMW001 G_M 经典白盖日用绵羊油 250g 0.39 3
1 3175905 2211450 3879243 AU21012341 Swisse 高含量维生素C泡腾片 60片 0.455 2
1 3175906 2211450 3879245 AUEN1560 Eaoron水光洁面Hyaluronic Cleanser 100ml 0.18 1
2 3175903 2211450 3879242 AU21012138 Freezeframe 祛皱纹眼袋黑眼圈精华眼霜 15ml 0.1 2
2 3175902 2211450 3879244 AU21012760 Trilogy趣乐活 天然原装有机野玫瑰果精油 45ml 0.1235 10
2 3175908 2211450 3879247 AUPA1004 Pure-Ora皮尔奥亚洲参人参胶囊 (30粒) 0.15 1
(7 row(s) affected)
*/