在一个表中查找与另一表匹配的分组记录

时间:2019-02-23 16:35:16

标签: sql hive

我在Hive中有两个表格,如下所示:

表1:

+----+----------+----------+-----------+
| id | subject  |   date   |     amount|
+----+----------+----------+-----------+
|  1 | Do this  | 10-10-13 |     20985 |
|  2 | Done this| 10-10-13 |     18657 |
|  3 | Dont do  | 12-12-13 |     22039 |
+----+----------+----------+-----------+

表2:

+----+----------+----------+-----------+
| id | subject  |   date   |     amount|
+----+----------+----------+-----------+
|  1 | Do this  | 10-10-13 |     10985 |
|  2 | Done this| 10-10-13 |     18657 |
|  3 | Dont do  | 12-12-13 |     22039 |
|  4 | Do this  | 10-10-13 |     10000 |
|  5 | Did this | 11-10-13 |     30000 |
+----+----------+----------+-----------+

当我在两个表上group by subject and date时,对于subject and date are Do this and 10-10-13的记录,我得到的总和(金额)相同。

现在,我想在table 2中找到要分组的记录,以匹配table 1中的分组量。

我想要的结果是

+----+----------+----------+-----------+
| id | subject  |   date   |     amount|
+----+----------+----------+-----------+
|  1 | Do this  | 10-10-13 |     10985 |
|  2 | Done this| 10-10-13 |     18657 |
|  3 | Dont do  | 12-12-13 |     22039 |
|  4 | Do this  | 10-10-13 |     10000 |
+----+----------+----------+-----------+

我如何实现自己想要的?

1 个答案:

答案 0 :(得分:2)

这是您想要的吗?

select t2.*
from (select t2.*,
             sum(amount) over (partition by date, subject) as total_amount
      from table2 t2
     ) t2 join
     table1 t1
     on t1.date = t2.date and t2.subject = t2.subject and t1.amount = t2.total_amount