HIVE点滴:group by和distinct语句的执行顺序
2016-07-11 20:01
302 查看
同一条语句之中,如果同时有group by和distinct语句,是先group by后distinct,还是先distinct后group by呢?
先说结论:先group by后distinct。
以下是在HIVE中的验证:
1)建表:其中xxx替换为本地目录名
create external table tmp_tb(
id int,
content int
) row format delimited
fields terminated by ','
stored as textfile
location '/tmp/xxx';
2)从tmp_tb文件中导入数据
load data
local inpath '/home/xxx/tmp_tb'
overwrite into table tmp_tb; tmp_tb内容:
1,5
2,6
2,5
2,5
3,6
3)仅有group by时:
select id, count(content)
from tmp_tb
group by id;结果如下:
1 1
2 3
3 1
4)同时有group by和distinct时:
select id, count(distinct content)
from tmp_tb
group by id;
结果如下:
1 1
2 2
3 1
可见,同时有group by和distinct时,显然是先group by 后distinct。如果是先distinct,后group by,则结果应该只有两条记录,因为content只有5和6两种数值。
先说结论:先group by后distinct。
以下是在HIVE中的验证:
1)建表:其中xxx替换为本地目录名
create external table tmp_tb(
id int,
content int
) row format delimited
fields terminated by ','
stored as textfile
location '/tmp/xxx';
2)从tmp_tb文件中导入数据
load data
local inpath '/home/xxx/tmp_tb'
overwrite into table tmp_tb; tmp_tb内容:
1,5
2,6
2,5
2,5
3,6
3)仅有group by时:
select id, count(content)
from tmp_tb
group by id;结果如下:
1 1
2 3
3 1
4)同时有group by和distinct时:
select id, count(distinct content)
from tmp_tb
group by id;
结果如下:
1 1
2 2
3 1
可见,同时有group by和distinct时,显然是先group by 后distinct。如果是先distinct,后group by,则结果应该只有两条记录,因为content只有5和6两种数值。
相关文章推荐
- HTML5基础学习笔记(五)
- 欢迎使用CSDN-markdown编辑器
- org.eclipse.jdt.core.prefs
- 给input文本框添加灰色提示文字
- 文章标题
- 哪些设计模式是降低资源使用率:----腾讯2016研发工程师笔试题(一)
- 前言介绍
- BZOJ3155 Preprefix sum
- java 集合类Array、List、Map区别和联系
- 我对DrawerLayout的了解
- POJ-1953-World Cup Noise
- C/C++遍历Lua中的table
- ScrollView与Listview滑动冲突解决
- android studio生成get和set方法
- 数据库索引的实现原理
- ios---类簇
- overflow与滚动条
- android横竖屏切换时候Activity的生命周期
- git使用
- mysql 批量修改某一个字符串 加前缀,后缀