MYSQL:显示来自两个不相关表的select语句中的一个查询

时间:2019-02-15 07:18:17

标签: mysql sql

我正在尝试显示不同的不相关表格的月收入总和,在这里我将预算与实际使用情况进行对比

我有两个表“计划预算”和“实际预算”,具有相似的列但数据不同,在查询中我想检索预算总和并将其按年和月分组。

计划预算表

+-----+-------+-------------+-------------+
| 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  |

实际预算

+-----+-------+-------------+---------------------+
| 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 |

我试图将它们水平组合,但是结果却是错误的

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 |
+------+-------+---------------+--------------+

我希望得到这样的结果:

+-----------+-------------+---------------+--------------+
| 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 |              |
+-----------+-------------+---------------+--------------+

2 个答案:

答案 0 :(得分:0)

您想做一个UNION ALL作为子查询,然后计算该子查询中值的总和,还要注意我在联合中使用了两个数量列,但其中一个总是为空,这是为了这两个选择返回的列数相同,但值分开

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

答案 1 :(得分:0)

您应该能够先对2个表进行UNION,然后从结果数据中进行SELECT,如下所示(更新查询):-

SELECT YEAR(date_period), MONTH(date_period), MonthlyBudget, MonthlyUsage
FROM 

(SELECT date_period, opca_budget as MonthlyBudget, 0 as MonthlyUsage FROM 
opexcapex
UNION 
SELECT date_period, 0 as MonthlyBudget, opca_budget as MonthlyUsage FROM 
opxcpx_actuals) T1

WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY YEAR(date_period), MONTH(date_period);