提示
Hive SQL 教程 欢迎使用。提供建议、纠错、催更等加作者微信: gr99123(备注:sql )和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
Impala SQL 方言支持一系列标准 SQL 元素,以及与数据加载和数据仓库相关的大数据用例的一些扩展。本页整理 Impala SQL 的些常见用法,以便随时备查,定期更新补充。
Impala SQL SELECT 的语法格式为:
关键词 | 表达式 | 说明 |
---|---|---|
WITH | name AS (select_expression) [, ...] ] | 公共表表达式 |
SELECT | [ALL/DISTINCT] [STRAIGHT_JOIN]/expression, exp2 | 输出内容 |
FROM | table_reference [, table_reference ...] | 指定查询表 |
JOIN | table_reference | 联接 |
ON | join_equality_clauses/USING (col1, col2) | 连接键 |
WHERE | conditions | 条件筛选 |
GROUP BY | col, / expression, | 分组 |
HAVING | conditions | 聚合值的排序 |
ORDER BY | col/expression [ASC/DESC] NULLS FIRST/NULLS LAST] | 排序 |
LIMIT | expression [OFFSET expression] | 输出限定的条数 |
UNION | [UNION [ALL] select_statement] ...] | 结果表连接 |
执行顺序:
FROM —> WHERE —> GROUP BY—> 聚合函数 —> HAVING—> SELECT —> ORDER BY —> LIMIT
类型 | 格式/举例 | 说明 |
---|---|---|
ARRAY | array<string> array<struct<name:string,age:int>> 每个元素有 .item/.pos |
任意数量有序元素(标量或其他复杂类型) |
BIGINT | -9223372036854775808 到 9223372036854775807 | 8字节整数数据类型 |
BOOLEAN | TRUE 或 false (大小写都可) | 单个真/假选择 |
CHAR | CHAR(4) | 固定长度文本,最长 255 |
DATE | 0001-01-01 到 9999-12-31 | 日期数据 |
DECIMAL | DECIMAL[(总长度[, 小数位])] | 固定长度和精度的数字 |
DOUBLE | 4.94065645841246544e-324d 到 1.79769313486231570e+308 正负 | 双精度浮点 |
FLOAT | 1.40129846432481707e-45 到 3.40282346638528860e+38 正负 | 单精度浮点 |
INT | -2147483648 到 2147483647 | 4字节整数 |
MAP | map<string,bigint> map<string,array<string>> |
任意一组键值对的复杂数据 |
REAL | cast(100.0 as double) -> 100 | DOUBLE 别名 |
SMALLINT | -32768 到 32767 | 2字节整数 |
STRING | CAST(3 AS STRING) | 字符串 |
STRUCT | struct<name:string,age:int> |
复杂的数据类型,常用为数组的元素类型或映射的值 |
TIMESTAMP | 1400-01-01 到 9999-12-31 | 日期和时间的值 |
TINYINT | -128 到 127 | 1 字节整数 |
VARCHAR | cast('world' as varchar(4)) | 可变长度的字符类型,最大长度为65535 |
Complex | ARRAY/MAP/STRUCT | 复杂类型(嵌套类型)单个行/列位置内表示多个数据值 |
注意:
-- 这一行是关于表的注释
create table ...;
/*
这是关于查询的多行注释
*/
select ...;
select * from t /* 这是关于查询的嵌入注释 */ where ...;
select * from t -- 这是多行命令中的尾随注释
where ...;
-- tinyint
select 10 as x;
-- smallint
select 4096 as x;
-- decimal(2,1)
select 1.5 as x;
-- decimal(4,3)
select 1.333 as x;
-- string
select "What\'s happening?" as single_within_double;
select 'I\'m not sure.' as single_within_single;
select "Homer wrote \"The Iliad\"." as double_within_double;
select 'Homer also wrote "The Odyssey".' as double_within_single;
-- Boolean
select true;
select * from t1 where assertion = false;
select case col when true then "yes" when false "no" else "null" end
from t1;
-- 时间
select CAST("2001-01-09 01:05:01" AS TIMESTAMP);
select CAST("2001-01-09T01:05:01" AS TIMESTAMP);
select CAST("1966‑07‑30" AS TIMESTAMP) + INTERVAL 5 YEARS;
还支持中文别名:
-- 中文别名
SELECT 10 as `金额`
字符:
- \t 表示一个 tab
-- 转化字符串 '20210901' 为时间类型
select to_timestamp(dt, 'yyyyMMdd') AS dt
select cast('1966-07-30' as timestamp);
select cast('1985-09-25 17:45:30.005' as timestamp);
select cast('08:30:00' as timestamp);
-- 15,返回小时
select hour('1970-01-01 15:30:00');
-- NULL 因为需要秒字段
select hour('1970-01-01 15:30');
-- NULL 因为小时值超出范围
select hour('1970-01-01 27:30:00');
-- 1 代表星期天
select dayofweek('2004-06-13');
-- Sunday
select dayname('2004-06-13');
-- 2005-06-13 hh:mm:ss 字段为零
select date_add('2004-06-13', 365);
-- 13
select day('2004-06-13');
-- 这两个日期之间有多少天?
select datediff('1989-12-31','1984-09-01');
-- 返回本地时区中的当前日期和时间
select now();
-- 2022-01-01 将时间类型转为指定格式字符串
select from_timestamp(registe_time, 'yyyy-MM-dd')
-- 窗口去重计数,注:不支持 count(distinct col) over(partition by ..)
dense_rank() over(partition by [dt] order by [math])
+ dense_rank() over(partition by [dt] order by [math] desc)
- 1
更新时间:2022-04-29 17:41:22 标签:impala sql 手册