Для работы Есть два запроса ,требуется репетитор который поможет человеку с базовыми знаниями подробно разобраться что происходит , объяснить структуру запроса и порядок выполнения и нюансы.. возможно несколько занятий ____________________ with main as (WITH RECURSIVE r AS (SELECT id, region_id FROM cell where region_id = ${region_id} and id = ${cell_id} UNION SELECT c.id, r.region_id FROM cell c JOIN r ON c.parent_id = r.id and c.region_id = r.region_id) SELECT * FROM r), with_children_cv as (select v_branch.name branch, count(distinct cv.volunteer_id) alls, count(distinct v.id) filter (where (extra ->> 'cfaaso')::text = 'Да') ready, count(distinct v.id) filter (where (extra ->> 'cfaauf')::text = 'Да') ready_region, count(distinct v.id) filter (where created_at >= '2023-01-01') new2023 from main join cell_volunteer cv on cv.cell_id = main.id join volunteer v on v.id = cv.volunteer_id join v_branch on v_branch.id = v.branch_id group by v_branch.name --total visitor child ), with_children_origin as (select v_branch.name branch, count(v.id) alls, count(v.id) filter (where (extra ->> 'cfaaso')::text = 'Да') ready, count(v.id) filter (where (extra ->> 'cfaauf')::text = 'Да') ready_region, count(v.id) filter (where created_at >= '2023-01-01') new2023 from main join volunteer v on v.origin_cell_id = main.id and v.region_id = main.region_id join v_branch on v_branch.id = v.branch_id group by v_branch.name -- uniq visitor child ) select wcv.branch as branch_name, coalesce(wcv.alls::int, 0) total, coalesce(wcv.ready::int, 0) ready, coalesce(wcv.ready_region::int, 0) ready_region, coalesce(wcv.new2023::int, 0) new_2023, coalesce(wco.alls::int, 0) origin, coalesce(wco.ready::int, 0) ready_origin, coalesce(wco.ready_region::int, 0) ready_region_origin, coalesce(wco.new2023::int, 0) new_2023_origin from with_children_cv wcv left join with_children_origin wco on wco.branch = wcv.branch order by wcv.branch