我有数据运输并乘以1个篮子,之前:
Pack ID Brand Part Ship Qty Qty per Basket divideval mod Batch
4 Brand A Part P 145 50 2 45 OB
4 Brand A Part P 125 50 2 25 OB2
在之后,我需要根据船舶数量/每个篮子的数量对数据进行多次计算:
Pack ID Brand Part Ship Qty Batch
4 Brand A Part P 50 OB
4 Brand A Part P 50 OB
4 Brand A Part P 45 OB
4 Brand A Part P 50 OB2
4 Brand A Part P 50 OB2
4 Brand A Part P 25 OB2
如何使用SQL Server制作它?
答案 0 :(得分:0)
由于您已经计算了Qty per Basket
,因此事情变得简单了。
该解决方案使用数字/计数表或递归cte生成一个。
以下查询使用数字表
select t.PackID, t.Brand, t.Part,
case when n = 1 and [mod] <> 0 then [mod] else t.QtyperBasket end as ShipQty,
t.Batch
from yourtable t
cross join numbers n
where n.n >= 1
and n.n <= t.divideval + case when [mod] > 0 then 1 else 0 end