UPDATE t SET rate = new.rate FROM ( WITH RECURSIVE cte(id, pid, rate) AS (SELECT id, pid, rate FROM t a UNION ALL SELECT cte.id, b.pid, b.rate * cte.rate / 100 FROM t b INNER JOIN cte ON b.id = cte.pid ) SELECT id, rate FROM cte WHERE pid = 0 ) new WHERE new.id = t.id;