Вы можете использовать этот запрос:
select x.Category Category, y.Estimated_Cost Estimated_Cost, x.Actual_Cost Actual_Cost
from
(
select t1.Category Category, sum(a.Actual_Cost) Actual_Cost
from Actuals a,
(select distinct Category from Budget) t1
where a.Task IN (Select Task from Budget where Category = t1.Category)
group by t1.Category
) x,
(
select Category, sum(Estimated_Cost) Estimated_Cost
from Budget
group by Category
) y
where x.Category = y.Category;
Иллюстрация с предоставленными данными:
select * from Budget;
+------+----------+----------------+
| Task | Category | Estimated_Cost |
+------+----------+----------------+
| 1 | 9100 | 100.00 |
| 2 | 9100 | 15.00 |
| 3 | 9100 | 6.00 |
| 4 | 9200 | 8.00 |
| 5 | 9200 | 11.00 |
+------+----------+----------------+
select * from Actuals;
+---------+------+-------------+
| Voucher | Task | Actual_Cost |
+---------+------+-------------+
| 1 | 1 | 10.00 |
| 2 | 1 | 20.00 |
| 3 | 1 | 15.00 |
| 4 | 2 | 32.00 |
| 5 | 4 | 8.00 |
| 6 | 5 | 3.00 |
| 7 | 5 | 4.00 |
+---------+------+-------------+
select x.Category Category, y.Estimated_Cost Estimated_Cost, x.Actual_Cost Actual_Cost
-> from
-> (
-> select t1.Category Category, sum(a.Actual_Cost) Actual_Cost
-> from Actuals a,
-> (select distinct Category from Budget) t1
-> where a.Task IN (Select Task from Budget where Category = t1.Category)
-> group by t1.Category
-> ) x,
-> (
-> select Category, sum(Estimated_Cost) Estimated_Cost
-> from Budget
-> group by Category
-> ) y
-> where x.Category = y.Category;
+----------+----------------+-------------+
| Category | Estimated_Cost | Actual_Cost |
+----------+----------------+-------------+
| 9100 | 121.00 | 77.00 |
| 9200 | 19.00 | 15.00 |
+----------+----------------+-------------+