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

时间:2018-11-25 18:25:42

标签: sql hsqldb

我正在将HSQLDB用于数据库,并且具有以下条件,在这种情况下,必须避免在连接2个表时重复条目。

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

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

预期输出如下

+-------------+------------+-------+-------+
| 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列值。

帮助...

3 个答案:

答案 0 :(得分:1)

这里的问题是,表中的日期有多个行。因此,我们将需要先在子查询中聚合它们。之后,它将用于执行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 

答案 1 :(得分:1)

一种解决此问题的方法是使用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;

答案 2 :(得分:0)

谢谢您的回答。 发布的两个答案都对我有用。

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;

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