Q:

MYSQL: Display one query from a select statement from two unrelated tables

I'm trying to display the sum of monthly revenue from different unrelated tables, Where i display the budgeted against the actual usage
I have two tables Planned Budget and actual budget, with with similar columns but different data, in a query i want retrieve the sum of budget and group it Yearly and monthly.
Planned Budget table
+-----+-------+-------------+-------------+
| uid | codes | opca_budget | date_period |
+-----+-------+-------------+-------------+
|  10 | 3210  | 3000        | 2018-03-01  |
|  17 | 3355  | 3000        | 2018-03-01  |
|  33 | 3210  | 4000        | 2018-04-01  |
|  40 | 3355  | 4000        | 2018-04-01  |
|  56 | 3210  | 5000        | 2018-05-01  |
|  63 | 3355  | 5000        | 2018-05-01  |
|  79 | 3210  | 6000        | 2018-06-01  |
|  86 | 3355  | 6000        | 2018-06-01  |
| 109 | 3355  | 45000       | 2018-07-01  |

Actual Budget
+-----+-------+-------------+---------------------+
| uid | codes | opca_budget | date_period         |
+-----+-------+-------------+---------------------+
|  10 | 3210  | 6500        | 2018-03-01 00:00:00 |
|  17 | 3355  | 6500        | 2018-03-01 00:00:00 |
|  18 | 3120  | 6500        | 2018-03-01 00:00:00 |
|  33 | 3210  | 7500        | 2018-04-01 00:00:00 |
|  40 | 3355  | 7500        | 2018-04-01 00:00:00 |
|  41 | 3120  | 7500        | 2018-04-01 00:00:00 |
|  56 | 3210  | 8500        | 2018-05-01 00:00:00 |
|  63 | 3355  | 8500        | 2018-05-01 00:00:00 |
|  64 | 3120  | 8500        | 2018-05-01 00:00:00 |

I tried to combine them horizontally, But i get wrong results
SELECT YEAR(c.date_period)
     , MONTH(c.date_period)
     , SUM(c.opca_budget) MonthlyBudget
     , SUM(a.opca_budget) MonthlyUsage    
  FROM opexcapex c
  LEFT
  JOIN opxcpx_actuals a
    ON YEAR(c.date_period) = YEAR(a.date_period) 
   AND MONTH(c.date_period) = MONTH(a.date_period) 
 WHERE c.date_period BETWEEN '2018-03-01' AND '2019-02-29' 
 GROUP 
    BY YEAR(c.date_period)
     , MONTH(c.date_period);

+------+-------+---------------+--------------+
| Year | Month | MonthlyBudget | MonthlyUsage |
+------+-------+---------------+--------------+
| 2018 |     3 |        168000 |       364000 |
| 2018 |     4 |        224000 |       420000 |
| 2018 |     5 |        280000 |       476000 |
| 2018 |     6 |        336000 |       532000 |
| 2018 |     7 |       2520000 |       588000 |
| 2018 |     8 |        576000 |       367200 |
| 2018 |     9 |       3240000 |       367200 |
| 2018 |    10 |        720000 |       252000 |
| 2018 |    11 |        792000 |      1807200 |
| 2018 |    12 |       2583000 |      1323000 |
| 2019 |     1 |          9000 |         NULL |
| 2019 |     2 |         36000 |         NULL |
+------+-------+---------------+--------------+

I expect to get results like this:
+-----------+-------------+---------------+--------------+
| BudgtYear | BudgetMonth | MonthlyBudget | MonthlyUsage |
+-----------+-------------+---------------+--------------+
|      2018 |           3 |         24000 |  45500       |
|      2018 |           4 |         32000 |  52500       |
|      2018 |           5 |         40000 |  59500       |
|      2018 |           6 |         48000 |  66500       |
|      2018 |           7 |        360000 |  73500       |
|      2018 |           8 |         72000 |  40800       |
|      2018 |           9 |        405000 |  40800       |
|      2018 |          10 |         90000 |  28000       |
|      2018 |          11 |         99000 | 200800       |
|      2018 |          12 |        369000 | 147000       |
|      2019 |           1 |          9000 |              |
|      2019 |           2 |         36000 |              |
+-----------+-------------+---------------+--------------+

A:

You want to do a UNION ALL as a sub-query and then calculate the sum on the value from that sub-query, notice also that I use two amount columns in the union but one is always null, this is to have the two selects return the same number of columns but separated values
SELECT YEAR(date_period) as year, MONTH(date_period) as month, SUM(mbudget) AS MonthlyBudget, SUM(musage) as MonthlyUsage
FROM (SELECT date_period, opca_budget mbudget, null musage
      FROM opexcapex 
      UNION ALL
      SELECT date_period, null, opca_budget 
      FROM opxcpx_actuals 
      ) sq
WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY year, month

