Не уверен, что для этого нужны оконные функции.
Приведенный ниже пример просто сам связывает таблицу и группы по продуктам C12.
INSERT INTO sales_performance
(product_ID,sales_period,sales_qty,sales_index,product_sub,goal_met)
VALUES
('C12','0001','15','20','D71','Y')
,('D71','0001','07','09','C12','N')
,('F20','0001','25','30','C05','Y')
,('C05','0001','10','15','F20','N')
,('C12','0002','15','30','C05','Y')
,('C05','0002','12','06','C12','N')
,('D71','0002','30','20','F20','Y')
,('F20','0002','20','15','D71','N')
,('C12','0003','05','04','F20','N')
,('F20','0003','40','35','C12','Y')
,('D71','0003','20','20','C05','Y')
,('C05','0003','12','10','D71','N')
;
SELECT
c12.*
, ROUND(AVG(sub.sales_index),1) AS sales_index_goal
FROM sales_performance c12
LEFT JOIN sales_performance sub
ON sub.sales_period < c12.sales_period
AND
(
(sub.product_ID = c12.product_sub AND sub.goal_met = 'N') OR
(sub.product_ID = c12.product_ID AND sub.goal_met = 'Y')
)
WHERE c12.product_id = 'C12'
GROUP BY c12.product_ID, c12.sales_period
ORDER BY c12.product_ID, c12.sales_period;
product_ID | sales_period | sales_qty | sales_index | product_sub | goal_met | sales_index_goal
:--------- | :----------- | :-------- | :---------- | :---------- | :------- | :---------------
C12 | 0001 | 15 | 20 | D71 | Y | <em>null</em>
C12 | 0002 | 15 | 30 | C05 | Y | 17.5
C12 | 0003 | 05 | 04 | F20 | N | 21.7