好久不写SQL了,有人知道怎么写这个分组的SQL吗?

zlp321002 2018-12-15 01:16:36
declare @t table (id int,oid int, detailid int, sku varchar(20),name varchar(120),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

select NTILE(2) over(partition by oid order by newid()) as groupCode,
*
from @t where oid=2211450


groupCode id oid detailid sku name qty
-------------------- ----------- ----------- ----------- -------------------- -------------------------------------------------- -----------
1 3175904 2211450 3879241 AU21012105 Pawpaw 天然木瓜膏 25克 0.1 10
1 3175906 2211450 3879245 AUEN1560 Eaoron水光洁面Hyaluronic Cleanser 100ml 0.18 1
1 3175907 2211450 3879246 AUGMW001 G_M 经典白盖日用绵羊油 250g 0.39 3
1 3175905 2211450 3879243 AU21012341 Swisse 高含量维生素C泡腾片 60片 0.455 2
2 3175903 2211450 3879242 AU21012138 Freezeframe 祛皱纹眼袋黑眼圈精华眼霜 15ml 0.1 2
2 3175902 2211450 3879244 AU21012760 Trilogy趣乐活 天然原装有机野玫瑰果精油 45ml 0 10
2 3175908 2211450 3879247 AUPA1004 Pure-Ora皮尔奥亚洲参人参胶囊 (30粒) 0.15 1


想根据QTY的任意组合值(之和不超过16)分组,比如:10,1,3,2 为 GroupCode=1; 2,10,1 为GroupCode=2;
现在想用一个公式计算不超过16的,现在用的是order by newid();

不知道有没有SQL能实现这需求; :D

...全文
125 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2018-12-15
  • 打赏
  • 举报
回复

-- 测试数据
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)
*/
唐诗三百首 2018-12-15
  • 打赏
  • 举报
回复
需求可能有点模糊, 每个分组的qty之和需尽量接近16吗?

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧