with tab1 as ( select * from (values (1, '1', 1, 2), (2, '1', 2, 1.4), (3, '1', 3, 5.2), (4, '2', 1, 0.6), (5, '2', 4, 0.5), (6, '2', 3, 0.9)) as tab1(idrow, group1, c1, c2)), t(group1, idrow, c1, c2, lvl) as ( select group1, min(idrow), (select cast(min(c1) as decimal(10, 4)) from tab1 where tab1.idrow=(select min(idrow) from tab1 where group1=tab1main.group1)), (select cast(min(c2) as decimal(10, 4)) from tab1 where tab1.idrow=(select min(idrow) from tab1 where group1=tab1main.group1)), 1 from tab1 tab1main group by group1 union all select tab1.group1, tab1.idrow, cast(tab1.c1*t.c2 + tab1.c2*t.c1 as decimal(10, 4)), cast(tab1.c2 * t.c2 as decimal(10,4)), lvl + 1 from tab1, t where tab1.group1 = t.group1 and tab1.idrow > t.idrow) select group1, (select min(c1) from t where group1=tmain.group1 and lvl=(select max(lvl) from t where group1=tmain.group1)) c1, (select min(c2) from t where group1=tmain.group1 and lvl=(select max(lvl) from t where group1=tmain.group1)) c2 from t tmain group by group1;