There is a company table. It stores the following data: ID, company name, budget, ID for the parent company (parent ID), the total amount. It is necessary to calculate the budget of the company + budget subsidiaries. I do this in the total amount column.
"SELECT o.id, o.title, o.budget, o.pid, (select sum(o1.budget)+o.budget from company o1 where o1.pid=o.id) total FROM company o ORDER BY o.id"; For this query, the amounts are displayed only one level lower, and I need to calculate the budgets of all the affiliated subsidiaries.