Let's start with some consumable test data which you should have provided:
CREATE TABLE #I
(
ProdID int NOT NULL
,Quantity int NOT NULL
);
INSERT INTO #I
VALUES (1001, 53)
,(1002, 32)
,(1003, 15);
CREATE TABLE #PO
(
POID int NOT NULL
,OrderDate date NOT NULL
);
INSERT INTO #PO
VALUES (2101, '20150605')
,(2102, '20160108')
,(2103, '20140903')
,(2104, '20160305')
,(2105, '20090109')
,(2106, '20110404');
CREATE TABLE #POD
(
PODID int NOT NULL
,POID int NOT NULL
,ProdID int NOT NULL
,QuantityOrdered int NOT NULL
);
INSERT INTO #POD
VALUES (3901, 2101, 1001, 10)
,(3902, 2102, 1001, 20)
,(3903, 2103, 1002, 30)
,(3904, 2104, 1002, 40)
,(3905, 2105, 1003, 10)
,(3906, 2106, 1003, 5);
I am not quite sure what you want but this should get you started:
WITH UnknownOrderDates
AS
(
SELECT I.ProdID, I.Quantity
,I.Quantity - SUM(POD.QuantityOrdered) AS QuantityOrdered
FROM #I I
JOIN #POD POD
ON I.ProdID = POD.ProdID
GROUP BY I.ProdID, I.Quantity
HAVING SUM(POD.QuantityOrdered) < I.Quantity
)
,ProductsOrdered
AS
(
SELECT ProdID, Quantity, QuantityOrdered, CAST(NULL AS date) AS OrderDate
FROM UnknownOrderDates
UNION ALL
SELECT I.ProdID, I.Quantity, POD.QuantityOrdered, PO.OrderDate
FROM #I I
JOIN #POD POD
ON I.ProdID = POD.ProdID
JOIN #PO PO
ON POD.POID = PO.POID
)
,ProductDateBalances
AS
(
SELECT ProdID, Quantity, QuantityOrdered, OrderDate
,Quantity - SUM(QuantityOrdered)
OVER (PARTITION BY ProdID ORDER BY OrderDate DESC ROWS UNBOUNDED PRECEDING) AS Balance
FROM ProductsOrdered
)
SELECT ProdID
,CASE
WHEN Balance >= 0
THEN QuantityOrdered
ELSE QuantityOrdered + Balance
END AS Quantity
,CAST(ROUND(DATEDIFF(day, OrderDate, CURRENT_TIMESTAMP)/365.25, 1) AS decimal(9,1)) AS Age
FROM ProductDateBalances
WHERE Quantity + Balance >= 0
AND (OrderDate <= DATEADD(year, -5, CURRENT_TIMESTAMP) OR OrderDate IS NULL);