add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
with cte as(
select 'A' supplier, 'shirt' product, 5 quantity, STR_TO_DATE('01/01/2019', '%m/%d/%Y') DateInserted
union all
select 'B' supplier, 'shirt' product, 5 quantity, STR_TO_DATE('01/29/2019', '%m/%d/%Y') DateInserted
union all
select 'C' supplier, 'shirt' product, 10 quantity, STR_TO_DATE('01/30/2019', '%m/%d/%Y') DateInserted
)
select * from(
select Supplier, Product, DateInserted, Quantity OldQuantity
,case when cumulsum <= TotalRequiredQuantity then 0 else cumulsum-TotalRequiredQuantity end NewQuantity
from(
select *, 7 TotalRequiredQuantity,
sum(quantity) over(partition by Product order by DateInserted ) cumulsum
from cte
)q
)q1
where OldQuantity>=NewQuantity;
Supplier Product DateInserted OldQuantity NewQuantity
A shirt 2019-01-01 5 0
B shirt 2019-01-29 5 3