欢迎大家扫码关注我的微信公众号:
Hive 之 查询 01-基本查询、where子句、分组
一、 基本查询
1.1 全表和特定列查询(select … from)
1.1.1 全表查询:
hive (default)> select * from dept;
OK
dept.deptno dept.dname dept.loc
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700
Time taken: 0.546 seconds, Fetched: 4 row(s)
1.1.2 特定列查询:
hive (default)> select dname, loc from dept;
OK
dname loc
ACCOUNTING 1700
RESEARCH 1800
SALES 1900
OPERATIONS 1700
Time taken: 0.077 seconds, Fetched: 4 row(s)
【注】
○ SQl 语言大小写不敏感;
○ SQL 可以写在一行或者多行;
○ 关键字不能被缩写也不能分行写;
○ 各子句一般要分行写;
○ 使用缩进提高语句的可读性;
1.2 列别名
1.2.1 重命名一个列;
1.2.2 便于计算;
1.2.3 紧跟列名, 【也可以】在列名和别名之间加入关键字 AS;
如:
hive (default)> select deptno AS dn, dname name from dept;
OK
dn name
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
Time taken: 0.07 seconds, Fetched: 4 row(s)
1.3 算数运算符
+ - * / % & | ^ ~
分别代表加、 减、 乘、 除、 取余、 按位与、 按位或、 按位异或、 按位取反;
如:
hive (default)> select * from dept;
OK
dept.deptno dept.dname dept.loc
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700
Time taken: 0.079 seconds, Fetched: 4 row(s)
hive (default)> select deptno * 10 dn from dept;
OK
dn
100
200
300
400
Time taken: 0.063 seconds, Fetched: 4 row(s)