将两个表中的两列相加而不用笛卡尔

Sum two columns from two tables without cartesian

我搜索了一段时间,但没有找到解决我问题的方法...

我有两个table:收入,支出

我有以下列:

我想汇总我所有收入的金额并按年和月显示它们,所以我这样做并且效果很好...

SELECT
     DATE_FORMAT(date, '%Y%m') AS 'date',
     SUM(amount) as 'total_earnings'
FROM earnings
GROUP BY DATE_FORMAT(date, '%Y%m')

输出类似于此...

date,total_earnings
201901,5000
201902,5500
201903,6200
etc.

我也是这样处理费用的 table 一切都很好。

我的问题: 当我尝试编写一个查询来显示按年和月分组的总收入和总支出时,我得到了一个显示 huuuge 数字的笛卡尔结果。

这是一个显示笛卡尔结果的示例查询:

SELECT
    DATE_FORMAT(ea.date, '%Y%m') AS date,
    SUM(ea.amount) AS 'total_earnings',
    SUM(ex.amount) AS 'total_expenses',
FROM earnings ea
    INNER JOIN expenses ex
        ON DATE_FORMAT(ea.date, '%Y%m') = DATE_FORMAT(ex.date, '%Y%m')
GROUP BY DATE_FORMAT(ex.date, '%Y%m')

如何在单个查询中显示每月收入和每月支出的总额?

期望输出:

date,total_earnings,total_expenses
201901,5000,1000
201902,5500,1100
201903,6200,1250

提前致谢!

也许您不需要连接..

drop table if exists t;
create table t
(expense_amount int,expense_date date, earnings_amount int, earnings_date date);

insert into t values
(10,'2020-01-01',null,null),
(10,'2020-01-01',null,null),
(10,'2020-02-01',null,null),
(null,null,10,'2020-01-01'),
(10,'2020-03-01',10,'2020-03-01');


select coalesce(expense_date,earnings_date) date,
         sum(case when expense_amount is not null then expense_amount else 0 end) expense,
         sum(case when earnings_amount is not null then earnings_amount else 0 end) earnings
from t
group by coalesce(expense_date,earnings_date);

+------------+---------+----------+
| date       | expense | earnings |
+------------+---------+----------+
| 2020-01-01 |      20 |       10 |
| 2020-02-01 |      10 |        0 |
| 2020-03-01 |      10 |       10 |
+------------+---------+----------+
3 rows in set (0.001 sec)

如果您的数据模型不同,因为 1 行可以包含不同日期的费用和收入,那么这对您不起作用,您应该单独生成总计并将它们合并。

需要先聚合,然后JOIN

SELECT ea.date, total_earnings, total_expenses
FROM (
    SELECT DATE_FORMAT(date, '%Y%m') AS date
        , SUM(amount) AS total_earnings
    FROM earnings 
    GROUP BY DATE_FORMAT(date, '%Y%m')
) ea
JOIN (
    SELECT DATE_FORMAT(date, '%Y%m') AS date
        , SUM(amount) AS total_expenses
    FROM expenses
    GROUP BY DATE_FORMAT(date, '%Y%m')
) ex ON ex.date = ea.date

您可以使用子查询实现:

SELECT `date`, SUM(earnings) AS total_earnings, SUM(expenses) AS total_expenses
FROM (
         SELECT DATE_FORMAT(earnings.date, '%Y%m') AS `date`, `amount` AS earnings, 0 AS expenses
         FROM earnings
         UNION
         SELECT DATE_FORMAT(expenses.date, '%Y%m') AS `date`, 0 AS earnings, `amount` AS expenses
         FROM expenses
     ) expenses_earnings
GROUP BY `date`

编辑:

使用 YEAR(date)MONTH(date) 而不是 DATE_FORMAT,这将允许您使用汇总计算每年的总数和总计。

SELECT `year`, `month`, SUM(earnings) AS total_earnings, SUM(expenses) AS total_expenses
FROM (
         SELECT YEAR(earnings.date) AS `year`, MONTH(earnings.date) AS `month`, `amount` AS earnings, 0 AS expenses
         FROM earnings
         UNION
         SELECT YEAR(expenses.date) AS `year`, MONTH(expenses.date) AS `month`, 0 AS earnings, `amount` AS expenses
         FROM expenses
     ) expenses_earnings
GROUP BY `year`, `month`