CREATE TABLE #aa(
item_sku NVARCHAR(50),--款号
item_size NVARCHAR(50),--尺码
md_code NVARCHAR(50),--门店编号
size_qty_avg NUMERIC(10,3),--尺码日均
need_qty NUMERIC(10,0))--需补数
INSERT #aa SELECT 'Y9815-T15-01','L','003','0.22','1'
INSERT #aa SELECT 'Y9815-T15-01','L','005','0.35','3'
INSERT #aa SELECT 'Y9815-T15-01','L','007','0.30','2'
INSERT #aa SELECT 'Y9815-T15-01','L','002','0.44','4'
INSERT #aa SELECT 'Y9815-T15-01','L','006','0.22','2'
表B
CREATE TABLE #bb(
item_sku NVARCHAR(50),--款号
item_size NVARCHAR(50),--尺码
zc_kc_qty int)--库存
INSERT #bb SELECT 'Y9815-T15-01','L','9'
求表C
表A
CREATE TABLE #cc(
item_sku NVARCHAR(50),--款号
item_size NVARCHAR(50),--尺码
md_code NVARCHAR(50),--门店编号
need_qty NUMERIC(10,0))--实补数
当表A的该款的所有need_qty小于等于表B的zc_kc_qty时,按need_qty插到表C。
当表A的该款的所有need_qty大于表B的zc_kc_qty时,先按尺码日均进行排序(从大到小),然后计算md_code数,当门店数大于zc_kc_qty时,所有门店实补数为1,从大到小顺序。不够的门店就为0。当门店数小于zc_kc_qty时,从大到小顺序分配zc_kc_qty以1为基础分配:
如:Y9815-T15-01的need_qty=10,小于表b zc_kc_qty,
排序:
Y9815-T15-01,L,002,0.44,4
Y9815-T15-01,L,005,0.35,3
Y9815-T15-01,L,007,0.30,2
Y9815-T15-01,L,003,0.22,1
Y9815-T15-01,L,001,0.22,1
首先分配为:
Y9815-T15-01,L,002 ,1
Y9815-T15-01,L,005,1
Y9815-T15-01,L,007,1
Y9815-T15-01,L,003,1
Y9815-T15-01,L,001,1
还有4个
再分配为
Y9815-T15-01,L,002,1
Y9815-T15-01,L,005,1
Y9815-T15-01,L,007,1
Y9815-T15-01,L,003,1
最后得出结果为
Y9815-T15-01,L,002,2
Y9815-T15-01,L,005,2
Y9815-T15-01,L,007,2
Y9815-T15-01,L,003,2
Y9815-T15-01,L,001,1
8 个解决方案
#1
declare @kc NUMERIC(10,1),--总库存数
@md NUMERIC(10,1),--总门店数
@a NVARCHAR(max),
@num INT ,
@num1 int,
@num2 int ,
@n INT,
@m INT
SELECT @kc =zc_kc_qty from # bb
SElect @md=COUNT(md_code) FROM #aa
SET @num=0
SET @num1=0
SET @num2=0
SET @n=1
IF @kc%@md=0 --能均分
SET @num=@kc/@md
ELSE--不能均分
SET @num1=@kc/@md+1
SET @m=@num1 * @n
while ((@kc - @m) % (@md - @n) != 0)
SET @n=@n+1;
SET @m= @num1 * @n
SET @num2 = (@kc - @m) / (@md - @n)
set @a='将' + CAST (@kc AS NVARCHAR(50)) + '条记录分成' + CAST (@md AS NVARCHAR(50))+ '组:'
if (@num != 0)
BEGIN
PRINT @a+'能平均分配,每组' + CAST (@num AS NVARCHAR(50))+ '个'
end
else
BEGIN
PRINT @a+'不能平均分配,前' + CAST (@n AS NVARCHAR(50)) + '组,每组' + CAST (@num1 AS NVARCHAR(50)) + '个,'
if (@n < @md - 1)
BEGIN
PRINT @a + '第' + CAST ((@n + 1)AS NVARCHAR(50)) + '组至第' + CAST (@md AS NVARCHAR(50))+ '组,每组' + CAST (@num2 AS NVARCHAR(50)) + '个.'
end
else
PRINT @a + '第' + CAST ((@n + 1)AS NVARCHAR(50)) + '组' + CAST (@num2 AS NVARCHAR(50)) + '个.'
end
不知道那里错了,当库存数量为9,可以分成5份,前4份为2,第5份为1. 但是当库存数为7,分成5份,却是前4份为2,第5份为1,而不是我想要的前2份为2,第3到第5份为1
#2
with tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
), tbl2
as
(
select * from tbl1
union all
select tbl2.sid,tbl2.item_sku,tbl2.item_size,tbl2.md_code,tbl2.size_qty_avg,tbl2.need_qty,tbl2.scount,tbl2.zc_kc_qty - tbl2.scount as zc_kc_qty,
tbl1.r_need_qty + case when tbl2.zc_kc_qty % tbl2.scount >= tbl2.sid then 1 else 0 end as rneed_qty
from tbl2, tbl1
where tbl1.item_sku= tbl2.item_sku and tbl1.sid = tbl2.sid and tbl2.zc_kc_qty - tbl2.scount > 0
)
select item_sku,item_size,md_code,r_need_qty
from tbl2
where zc_kc_qty /scount = 0
order by sid
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
), tbl2
as
(
select * from tbl1
union all
select tbl2.sid,tbl2.item_sku,tbl2.item_size,tbl2.md_code,tbl2.size_qty_avg,tbl2.need_qty,tbl2.scount,tbl2.zc_kc_qty - tbl2.scount as zc_kc_qty,
tbl1.r_need_qty + case when tbl2.zc_kc_qty % tbl2.scount >= tbl2.sid then 1 else 0 end as rneed_qty
from tbl2, tbl1
where tbl1.item_sku= tbl2.item_sku and tbl1.sid = tbl2.sid and tbl2.zc_kc_qty - tbl2.scount > 0
)
select item_sku,item_size,md_code,r_need_qty
from tbl2
where zc_kc_qty /scount = 0
order by sid
#3
你把结果insert到#cc就行了
#4
如果#aa #bb表的关联字段还有item_size,自己加一下吧
#5
这个没有循环,当库存为11,门店数为5时,不是3,2,2,2,2
#6
我发现这个问题了,正在看,余数的问题
#7
with tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
)
select item_sku,item_size,md_code,zc_kc_qty/scount + case when zc_kc_qty % scount >= sid then 1 else 0 end need_qty from tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
)
select item_sku,item_size,md_code,zc_kc_qty/scount + case when zc_kc_qty % scount >= sid then 1 else 0 end need_qty from tbl1
#8
脑袋刚才不好使,想复杂了
#1
declare @kc NUMERIC(10,1),--总库存数
@md NUMERIC(10,1),--总门店数
@a NVARCHAR(max),
@num INT ,
@num1 int,
@num2 int ,
@n INT,
@m INT
SELECT @kc =zc_kc_qty from # bb
SElect @md=COUNT(md_code) FROM #aa
SET @num=0
SET @num1=0
SET @num2=0
SET @n=1
IF @kc%@md=0 --能均分
SET @num=@kc/@md
ELSE--不能均分
SET @num1=@kc/@md+1
SET @m=@num1 * @n
while ((@kc - @m) % (@md - @n) != 0)
SET @n=@n+1;
SET @m= @num1 * @n
SET @num2 = (@kc - @m) / (@md - @n)
set @a='将' + CAST (@kc AS NVARCHAR(50)) + '条记录分成' + CAST (@md AS NVARCHAR(50))+ '组:'
if (@num != 0)
BEGIN
PRINT @a+'能平均分配,每组' + CAST (@num AS NVARCHAR(50))+ '个'
end
else
BEGIN
PRINT @a+'不能平均分配,前' + CAST (@n AS NVARCHAR(50)) + '组,每组' + CAST (@num1 AS NVARCHAR(50)) + '个,'
if (@n < @md - 1)
BEGIN
PRINT @a + '第' + CAST ((@n + 1)AS NVARCHAR(50)) + '组至第' + CAST (@md AS NVARCHAR(50))+ '组,每组' + CAST (@num2 AS NVARCHAR(50)) + '个.'
end
else
PRINT @a + '第' + CAST ((@n + 1)AS NVARCHAR(50)) + '组' + CAST (@num2 AS NVARCHAR(50)) + '个.'
end
不知道那里错了,当库存数量为9,可以分成5份,前4份为2,第5份为1. 但是当库存数为7,分成5份,却是前4份为2,第5份为1,而不是我想要的前2份为2,第3到第5份为1
#2
with tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
), tbl2
as
(
select * from tbl1
union all
select tbl2.sid,tbl2.item_sku,tbl2.item_size,tbl2.md_code,tbl2.size_qty_avg,tbl2.need_qty,tbl2.scount,tbl2.zc_kc_qty - tbl2.scount as zc_kc_qty,
tbl1.r_need_qty + case when tbl2.zc_kc_qty % tbl2.scount >= tbl2.sid then 1 else 0 end as rneed_qty
from tbl2, tbl1
where tbl1.item_sku= tbl2.item_sku and tbl1.sid = tbl2.sid and tbl2.zc_kc_qty - tbl2.scount > 0
)
select item_sku,item_size,md_code,r_need_qty
from tbl2
where zc_kc_qty /scount = 0
order by sid
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
), tbl2
as
(
select * from tbl1
union all
select tbl2.sid,tbl2.item_sku,tbl2.item_size,tbl2.md_code,tbl2.size_qty_avg,tbl2.need_qty,tbl2.scount,tbl2.zc_kc_qty - tbl2.scount as zc_kc_qty,
tbl1.r_need_qty + case when tbl2.zc_kc_qty % tbl2.scount >= tbl2.sid then 1 else 0 end as rneed_qty
from tbl2, tbl1
where tbl1.item_sku= tbl2.item_sku and tbl1.sid = tbl2.sid and tbl2.zc_kc_qty - tbl2.scount > 0
)
select item_sku,item_size,md_code,r_need_qty
from tbl2
where zc_kc_qty /scount = 0
order by sid
#3
你把结果insert到#cc就行了
#4
如果#aa #bb表的关联字段还有item_size,自己加一下吧
#5
这个没有循环,当库存为11,门店数为5时,不是3,2,2,2,2
#6
我发现这个问题了,正在看,余数的问题
#7
with tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
)
select item_sku,item_size,md_code,zc_kc_qty/scount + case when zc_kc_qty % scount >= sid then 1 else 0 end need_qty from tbl1
as
(
select ROW_NUMBER() over (partition by a.item_sku order by size_qty_avg desc) as sid,
a.*,b.scount,c.zc_kc_qty, case when zc_kc_qty > scount then 1 else 0 end as r_need_qty
from #aa a cross apply (select count(1) as scount from #aa where item_sku = a.item_sku) b
join #bb c on a.item_sku = c.item_sku
)
select item_sku,item_size,md_code,zc_kc_qty/scount + case when zc_kc_qty % scount >= sid then 1 else 0 end need_qty from tbl1
#8
脑袋刚才不好使,想复杂了