根据除法运算的结果拆分行

Split row based on result of divide operation

我有 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服务器实现?

因为你已经计算了Qty per Basket,所以事情会简单一些。

解决方案使用数字/计数table或递归cte来生成一个。

下面的查询使用了一个数字table

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