使用带 SUM 和 GROUP BY 的完整 JOIN 避免重复条目

Avoid duplicate entries using full JOIN with SUM and GROUP BY

我正在为数据库使用 HSQLDB,并且遇到以下情况,在加入 2 table 时我必须避免重复条目。

表 1

HMEXPENSE
+--------+---------------+-------------+
| USERID | EXPENSEAMOUNT | EXPENSEDATE |
+--------+---------------+-------------+
|      a |      100      | 2018-10-10  |
|      a |      200      | 2018-10-11  |
|      a |      100      | 2018-10-11  |
|      a |      200      | 2018-10-13  |
+--------+---------------+-------------+

表 2

HMINCOME
+--------+---------------+-------------+
| USERID | EXPENSEAMOUNT | EXPENSEDATE |
+--------+---------------+-------------+
|      a |      200      | 2018-10-10  |
|      a |      100      | 2018-10-11  |
|      a |      200      | 2018-10-11  |
|      a |      100      | 2018-10-12  |
+--------+---------------+-------------+

给我重复条目的当前查询如下

SELECT e.expenseDate ,i.incomeDate , SUM(e.expenseAmount), SUM(i.incomeAmount)
FROM HMINCOME i FULL JOIN HMEXPENSE e on i.incomeDate = e.expenseDate 
GROUP BY i.incomeDate,e.expenseDate, i.incomeAmount, e.expenseAmount

输出

+-------------+------------+-------+-------+
| EXPENSEDATE | INCOMEDATE |   C3  |   C4  |
+-------------+------------+-------+-------+
|  2018-10-10 | 2018-10-10 | 100.0 | 200.0 |
|  2018-10-11 | 2018-10-11 | 200.0 | 100.0 |
|  2018-10-11 | 2018-10-11 | 100.0 | 100.0 |
|  2018-10-11 | 2018-10-11 | 200.0 | 200.0 |
|  2018-10-11 | 2018-10-11 | 100.0 | 200.0 |
|   <null>    | 2018-10-12 | <null>| 100.0 |
|  2018-10-13 |   <null>   | 200.0 | <null>|
+-------------+------------+-------+-------+

如果我使用上面提到的这个查询来得到我实际场景中需要的实际输出如下

SELECT e.expenseDate, i.incomeDate , SUM(e.expenseAmount),SUM(i.incomeAmount)
FROM HMINCOME i FULL JOIN HMEXPENSE e on i.incomeDate = e.expenseDate 
GROUP BY i.incomeDate,e.expenseDate

输出

+-------------+------------+-------+-------+
| EXPENSEDATE | INCOMEDATE |   C3  |   C4  |
+-------------+------------+-------+-------+
|  2018-10-10 | 2018-10-10 | 100.0 | 200.0 |
|  2018-10-11 | 2018-10-11 | 600.0 | 600.0 |
|   <null>    | 2018-10-12 | <null>| 100.0 |
|  2018-10-13 |   <null>   | 200.0 | <null>|
+-------------+------------+-------+-------+

要求是获取一天的总金额和另一天 table 中不存在的日期的空条目。

预期输出如下

+-------------+------------+-------+-------+
| EXPENSEDATE | INCOMEDATE |   C3  |   C4  |
+-------------+------------+-------+-------+
|  2018-10-10 | 2018-10-10 | 100.0 | 200.0 |
|  2018-10-11 | 2018-10-11 | 300.0 | 300.0 |
|   <null>    | 2018-10-12 | <null>| 100.0 |
|  2018-10-13 |   <null>   | 200.0 | <null>|
+-------------+------------+-------+-------+

由于条目重复,C3 和 C4 列值计算不正确。

求助...

这里的问题是 table 中的日期有多行。因此,我们需要首先在子查询中聚合它们。之后,它将被用来做 FULL JOIN.

尝试:

SELECT 
  e.expenseDate,
  i.incomeDate, 
  e.sumExpenseAmount, 
  i.sumIncomeAmount
FROM 
(SELECT incomeDate, SUM(incomeAmount) sumIncomeAmount
 FROM HMINCOME
 GROUP BY incomeDate) i
FULL JOIN 
(SELECT expenseDate, SUM(expenseAmount) sumExpenseAmount
 FROM HMEXPENSE
 GROUP BY expenseDate) e
  ON i.incomeDate = e.expenseDate 

解决此问题的一种方法是使用 union allgroup by:

select dte, sum(incomeamount) as incomeamount, sum(expenseamount) as expenseamount
from ((select incomedate as dte, incomeamount, 0 as expenseamount
       from hmincome
      ) union all
      (select expensedate, 0, expenseAmount
       from hmexpense
      )
     ) ie
group by dte
order by dte;