hiveQL求差集

hive sql求差集的方法

1、什么是差集

set1 - set2,即去掉set1中存在于set2中的数据。

2、hive中计算差集的方法,基本是使用左外链接。

直接上代码

select * from table1 t1 left outer join table2 t2 on t1.id = t2.id where t2.id = null;

3、一般来说我们要先去重,使得两个表都变成集合,元素唯一。

先对table2(右表)去重然后再计算差集。

select * from 
  (
    select * from table1 where year=2017 and month=07 and day=01
  ) t1
left outer join
  (
    select * from (select *,row_number() over(partition by id) num from table2 where year=2017 and month=07 and day=01) t where t.num =1) t2
on t1.id = t2.id where t2.id==null;
原文地址:https://www.cnblogs.com/kangoroo/p/7116001.html