DROP VIEW IF EXISTS total_team_and_overall_margin_child_view;
CREATE VIEW total_team_and_overall_margin_child_view (
 parent_id_project,
 total_team_cost,
 total_cost_copy_scapes,
 total_cost_extra,
 total_costs
) as
SELECT
    projects.parent_id_project,
    SUM(COALESCE(team_offers_project_list_view.total_team_amount, 0 ) + COALESCE(total_admin_amout_view.total_admin_amount, 0))  as total_team_cost,
    SUM(COALESCE(copy_space_project_list_view.copyscape_cost_project,0 )) as total_cost_copy_scapes,
    SUM(COALESCE(additional_cost_list_view.additional_costs, 0)) as total_cost_extra,
    SUM(COALESCE(team_offers_project_list_view.total_team_amount, 0) + COALESCE(total_admin_amout_view.total_admin_amount, 0) + COALESCE(copy_space_project_list_view.copyscape_cost_project, 0) + COALESCE( additional_cost_list_view.additional_costs, 0)) as total_costs
FROM
    projects
LEFT JOIN project_offers offers ON projects.id = offers.project_id
LEFT JOIN team_amount_project_list_view ON team_amount_project_list_view.offer_id = offers.id
LEFT JOIN prices_categories_project_list_view ON prices_categories_project_list_view.project_id = projects.id
LEFT JOIN team_offers_project_list_view ON team_offers_project_list_view.project_id = projects.id
LEFT JOIN copy_space_project_list_view ON copy_space_project_list_view.project_id = projects.id
LEFT JOIN additional_cost_list_view ON additional_cost_list_view.project_id = projects.id
LEFT JOIN total_admin_amout_view ON total_admin_amout_view.project_id = projects.id
where projects.parent_id_project > 0
GROUP BY
    projects.parent_id_project;