Hive Select语句

select

Hive-SQL的基本结构同标准SQL相差不大,结构如下:

select * from employee 
where sex_age.sex='Male'

/*支持使用distinct去重*/
select distinct name,sex_age.sex as sex from employee;
select distinct * from employee; // supported after release 1.1.0

/*hive 0.7.0之后,支持having子句*/
SELECT col1 FROM t1 GROUP BY col1 HAVING SUM(col2) > 10;

/*支持limit子句,限制返回行数*/
SELECT * FROM t1 LIMIT 5; // 随机返回5行

/*返回Top K行*/
SET mapred.reduce.tasks = 1
SELECT * FROM sales SORT BY amount DESC LIMIT 5

在Hive-SQL中,也支持嵌套查询及子查询,但是,不允许出现多重嵌套或多重子查询的情况,常用的子查询方式如下:

/*通过with语句,子查询必须申明别名*/
with t1 as (
select * from employee 
where sex_age.sex='Male'
)
select name,work_place from t1;

/*子查询放置在from子句,子查询必须申明别名*/
select name,sex_age.sex from
(
select * from employee 
where sex_age.sex='Male'
) t1;

/*
子查询放置在where子句,在where子句中的子查询支持in,not in,exists,not exists,但需要注意的是,使用in或not in时,
子查询只能返回一个字段,且使用in/not in不能同时涉及内/外部表
*/
select name,sex_age.sex from
employee a // 外部表必须申明别名,不然Hive报错
where a.name in (
select name from employee
where sex_age.sex='Male'
);

/*
ERROR: 
in/not in不能同时涉及内/外部表,exists/not exists没有这个限制
*/
select name,sex_age.sex from
employee a
where a.name in (
select name from employee
where sex_age.sex!=a.sex_age
);

另外,where子句可以支持任何Boolean表达式及User Define Function。

Partition Based Queries

想要触发hive的分区查询,必须满足以下两个条件:
- 建表时使用了partitioned by
- where条件中包含分区字段,或者join语句的on子句包含分区字段
因为hive只会扫描特定分区的数据,所以,分区查询可以提高查询的速度。

REGEX Column Specification

hive支持通过正则表达式来声明查询字段。

SELECT `(ds|hr)?+.+` FROM sales
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值