差集定义:一般地,设A,B是两个集合,由所有属于A且不属于B的元素组成的集合,叫做集合A减集合B(或集合A与集合B之差)。

类似地,对于集合A,B,我们把集合{x/x∈A,且x¢B}叫做A与B的差集,记作A-B记作A-B(或A\B);

即A-B={x|x∈A,且x ¢B}(或A\B={x|x∈A,且x ¢B} B-A={x/x∈B且x¢A} 叫做B与A的差集。

比如说有这么两个表:

hive> select * from A;
OK
1 2
1 3
2 1
2 3
3 1
Time taken: 0.3 seconds, Fetched: 5 row(s)
hive> select * from B;
OK
1 2
1 4
2 2
2 3
Time taken: 0.086 seconds, Fetched: 4 row(s)

  

要取出A与B的差集(A-B):

1	3
2 1
3 1

  

Hive可不可以用not in?可以,但只能用于单个字段。select * from A where (uid,goods) not in (select uid,goods from B);这个oracle是支持的,但hive不行。

hive> select * from A  where uid not in (select uid from B);
3 1
Time taken: 46.09 seconds, Fetched: 1 row(s)

  

Hive可不可以用not exists?显然也可以! 

hive> select * from A  where not exists (select * from B where A.uid=B.uid and A.goods=B.goods);
1 3
2 1
3 1
Time taken: 12.989 seconds, Fetched: 3 row(s)

  

不过前两种貌似很费资源,在ODPS里都有限制,下面来介绍一下hive常用的求差集方法,左(右)连接 left outer join

 

先看一下左连接之后表是什么样的

hive> select * from A a left outer join B b on a.uid=b.uid and a.goods=b.goods;
1 2 1 2
1 3 NULL NULL
2 1 NULL NULL
2 3 2 3
3 1 NULL NULL
Time taken: 12.735 seconds, Fetched: 5 row(s)

  

现在只要取出B的uid和goods为null的行就可以了

hive> select a.* from A a left outer join B b on a.uid=b.uid and a.goods=b.goods where b.uid is null and b.goods is null;
1 3
2 1
3 1
Time taken: 13.023 seconds, Fetched: 3 row(s)

  

转自:https://blog.csdn.net/Dr_Guo/article/details/51182626

最新文章

  1. objective-c 语法快速过(5)
  2. [转]jQuery的each方法的几种常用的用法
  3. TimeQuest 静态时序分析 基本概论
  4. Spring+SpringMVC+Mybatis大整合(SpringMVC采用REST风格、mybatis采用Mapper代理)
  5. 纯CSS实现3D按钮效果
  6. JS高程1.javascript简介
  7. iOS - CADisplayLink与NSTimer
  8. iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet
  9. [小知识]不显示没有内容的UITableViewCell
  10. 如何将VMware虚拟机迁移到AWS
  11. 指令-arContentedit-可编辑的高度自适应的div
  12. CMS垃圾收集器
  13. css-改变input原始样式
  14. fyi
  15. JAVA-Enum 枚举
  16. Android 开发 创建WiFi、WiFi热点 ---开发集合
  17. Reading Task 2 —— by12061154Joy
  18. BZOJ2440 中山市选2011完全平方数(容斥原理+莫比乌斯函数)
  19. lua------------------Unity3D研究院编辑器之打开unity不可识别的文件(十三)
  20. UVALive - 7261 Xiongnu's Land

热门文章

  1. CF 862A Mahmoud and Ehab and the MEX【数组操作】
  2. 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
  3. poj2778(AC 自动机)
  4. window下安装rsyncServer
  5. 小程序使用npm模块(引入第三方UI),报错的多种解决办法。
  6. 手动编译含package的java源程序(包含外部包中定义的类)
  7. iOS开发——使用Autolayout生成动态高度的TableViewCell单元格
  8. menuStrip鼠标经过自动显示菜单
  9. Silverlight游戏设计(Game Design):(十四)练习用游戏素材资源的获取及相关工具使用心得 --转
  10. C++游戏界面不流畅的问题