Вы можете попробовать это
DECLARE @Inventor TABLE (ItemCode VARCHAR(10), InventoryDate DATE, InventoryQty INT)
INSERT @Inventor VALUES
('Product1', '2019-07-05', 12),
('Product1', '2019-07-12', 3 ),
('Product1', '2019-08-03', 0 ),
('Product1', '2019-08-15', 7 ),
('Product1', '2019-08-29', 0 ),
('Product1', '2019-09-02', 9 )
SELECT StIn.ItemCode, StOut.InventoryDate StockOut , StIn.InventoryDate StockIn FROM @Inventor StOut
CROSS APPLY (SELECT TOP 1 * FROM @Inventor S WHERE StOut.ItemCode = S.ItemCode
AND S.InventoryDate > StOut.InventoryDate
AND S.InventoryQty > 0
ORDER BY S.InventoryDate
) StIn
WHERE StOut.InventoryQty = 0
Результат:
ItemCode StockOut StockIn
---------- ---------- ----------
Product1 2019-08-03 2019-08-15
Product1 2019-08-29 2019-09-02