1、等值比较:=
 2、不等值比较:<>
 3、小于比较:<
 4、小于等于比较:<=
 5、大于比较:>
 6、大于等于比较:>
 7、空值判断:IS NULL
 8、非空判断:IS NOT NULL
 9、LIKE 比较:LIKE
 10、JAVA 的 LIKE 比较:RLIKE
 11、正则匹配:REGEXP
 数学运算
 1、加法操作:+
 2、减法操作:-
 3、乘法操作:*
 4、除法操作:/
 5、取余操作:%
 6、位与操作:&
 7、位或操作:|
 8、位异或操作:^
 9、按位取反操作:~
 逻辑运算
 1、逻辑与操作:AND
 2、逻辑与操作:OR
 3、逻辑非操作:NOT
 条件运算
 1、IF 函数:IF
 2、非空查找函数:COALESCE
 3、条件判断函数:CASE
 4、条件判断函数:CASE
 数值计算
 1、近似函数:round
 2、指定精度近似函数:round
 3、向下取整函数:floor
 4、向上取整函数:ceil
 5、向上取整函数:ceiling
 6、取随机函数:rand
 7、自然指数函数:exp
 8、以 10 为底的对数函数:log10
 9、以 2 为底的对数函数:log2
 10、对数函数:log
 11、幂运算函数:pow
 12、幂运算函数:power
 13、平方根函数:sqrt
 14、二进制函数:bin
 15、十六进制函数:hex
 16、反转十六进制函数:unhex
 17、进制转换函数:conv
 18、绝对值函数:abs
 19、正取余函数:pmod
 20、正弦函数:sin
 21、反正弦函数:asin
 22、余弦函数:cos
 23、反余弦函数:acos
 24、positive 函数:positive
 25、negative 函数:negative
 日期函数
 1、UNIX 时间戳转日期函数:fom_unixtime
 2、获取当前 UNIX 时间戳函数:unix_timestamp
 3、日期转 UNIX 时间戳函数:unix_timestamp
 4、指定格式日期转 UNIX 时间戳函数:unix_timestamp
 5、日期时间转日期函数:to_date
 6、日期转年函数:year
 7、日期转月函数:month
 8、日期转天函数:day
 9、日期转小时函数:hour
 10、日期转分钟函数:minute
 11、日期转秒函数:second
 12、日期转周函数:weekofyear
 13、日期比较函数:datediff
 14、日期增加函数:date_add
 15、日期减少函数:date_sub
 字符串函数
 1、字符串长度函数:length
 2、字符串反转函数:reverse
 3、字符串连接函数:concat
 4、带分隔符字符串连接函数:concat_ws
 5、字符串截取函数:substr | substring
 6、字符串截取函数:substr | substring
 7、字符串转大写函数:upper | ucase
 8、字符串转小写函数:lower | lcase
 9、去空格函数:trim
 10、左边去空格函数:ltrim
 11、右边去空格函数:rtrim
 12、正则表达式替换函数:regexp_replace
 13、正则表达式解析函数:regexp_extract
 14、URL 解析函数:parse_url
 15、JSON 解析函数:get_json_object
 16、空字符串函数:space
 17、重复字符串函数:repeat
 18、首字符 ASCII 函数:ascii
 19、左补足函数:lpad
 20、右补足函数:rpad
 21、分割字符串函数:split
 22、集合查找函数:find_in_set
 聚合函数
 1、统计函数:count
 2、求和函数:sum
 3、平均值函数:avg
 4、最小值函数:min
 5、最大值函数:max
 6、总体方差函数:var_pop
 7、样本方差函数:var_samp
 8、总体标准差函数:stddev_pop
 9、样本标准差函数:stddev_samp
 10、百分位函数:percentile
 11、百分位函数:percentile
 12、近似百分位函数:percentile_approx
 13、近似百分位函数:percentile_approx
 14、直方图:histogram_numeric
 在 hive 中可通过以下命令查看函数信息:
--显示所有的可用函数,包括运算符、内置函数、自定义函数
 show functions;
--显示指定函数的描述信息
 desc function trim;
--显示指定函数的详细信息
 desc function extended trim;
 1
 2
 3
 4
 5
 6
 7
 8
  
关系运算
  
1、等值比较:=
 语法:A = B
 操作类型:基本类型
 描述:如果表达式 A 与表达式 B 相等,则为 TRUE;否则为 FALSE。
select 1 where 1 = 1;
 1
 1
 1
 2、不等值比较:<>
 语法:A <> B
 操作类型:基本类型
 描述:如果表达式 A 或表达式 B 为 NULL,则返回 NULL;如果表达式 A 与 表达式 B 不相等,则为 TRUE;否则为 FALSE。
select 1 where 1 <> 2;
 1
 1
 1
 3、小于比较:<
 语法:A < B
 操作类型:基本类型
 描述:如果表达式 A 或表达式 B 为 NULL,则返回 NULL;如果表达式 A 小于表达式 B,则为 TRUE;否则为 FALSE。
select 1 where 1 < 2;
 1
 1
 1
 4、小于等于比较:<=
 语法:A <= B
 操作类型:基本类型
 描述:如果表达式 A 或表达式 B 为 NULL,则返回 NULL;如果表达式 A 小于或者等于表达式 B,则为 TRUE;否则为 FALSE。
select 1 where 1 <= 1;
 1
 1
 1
 5、大于比较:>
 语法:A > B
 操作类型:基本类型
 描述:如果表达式 A 或表达式 B 为 NULL,则返回 NULL;如果表达式 A 大于表达式 B,则为 TRUE;否则为 FALSE。
select 1 where 2 > 1;
 1
 1
 1
 6、大于等于比较:>
 语法:A >= B
 操作类型:基本类型
 描述:如果表达式 A 或表达式 B 为 NULL,则返回 NULL;如果表达式 A 大于或者等于表达式 B,则为 TRUE;否则为 FALSE。
select 1 where 1 >= 1;
 1
 1
 1
 7、空值判断:IS NULL
 语法:A IS NULL
 操作类型:所有类型
 描述:如果表达式 A 的值为 NULL,则为 TRUE;否则为 FALSE。
select 1 where null is null;
 1
 1
 1
 8、非空判断:IS NOT NULL
 语法:A IS NOT NULL
 操作类型:所有类型
 描述:如果表达式 A 的值为 NULL,则为 FALSE;否则为 TRUE。
select 1 where 1 is null;
 1
 1
 1
 9、LIKE 比较:LIKE
 语法:A LIKE B
 操作类型:字符类型
 描述:如果字符串 A 或字符串 B 为 NULL,则返回 NULL;如果字符串 A 符合简单 SQL 正则表达式 B 的语法,则为 TRUE;否则为 FALSE。
字符 ”_” 表示任意单个字符
select 1 where 'football' like 'foot____';
 1
 1
 1
 字符 ”%” 表示任意数量的字符
select 1 where 'football' like 'foot%';
 1
 1
 1
 注意:否定比较时候用 NOT A LIKE B
select 1 where not 'football' like 'fff%';
 1
 1
 1
 10、JAVA 的 LIKE 比较:RLIKE
 语法:A RLIKE B
 操作类型:字符类型
 描述:如果字符串 A 或字符串 B 为 NULL,则返回 NULL;如果字符串 A 符合 JAVA 正则表达式 B 的正则语法,则为 TRUE;否则为 FALSE。
select 1 where 'football' rlike '^f.*l$';
 1
 1
 1
 11、正则匹配:REGEXP
 语法:A REGEXP B
 操作类型:字符类型
 描述:功能与 RLIKE 相同
select 1 where 'football' regexp '^f.*l$';
 1
 1
 1
  
数学运算
  
1、加法操作:+
 语法:A + B
 操作类型:数值类型
 说明:返回 A 与 B 相加的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。比如,int + int 一般结果为 int 类型,而 int + double 一般结果为 double 类型。
select 1 + 2;
 1
 3
 1
 select 1 + 0.5;
 1
 1.5
 1
 2、减法操作:-
 语法:A - B
 操作类型:数值类型
 说明:返回 A 与 B 相减的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。比如,int - int 一般结果为 int 类型,而 int - double 一般结果为 double 类型。
select 3 - 2;
 1
 1
 1
 select 1.5 - 1;
 1
 0.5
 1
 3、乘法操作:*
 语法:A * B
 操作类型:数值类型
 说明:返回 A 与 B 相乘的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。注意,如果 A 乘以 B 的结果超过默认结果类型的数值范围,则需要通过 cast 将结果转换成范围更大的数值类型。
select 3 * 8;
 1
 24
 1
 4、除法操作:/
 语法:A / B
 操作类型:数值类型
 说明:返回 A 除以 B 的结果。结果的数值类型为 double。
select 10 / 5;
 1
 2.0
 1
 注意:hive 中最高精度的数据类型是 double,只精确到小数点后16位,在做除法运算的时候要特别注意
5、取余操作:%
 语法:A % B
 操作类型:数值类型
 说明:返回 A 除以 B 的余数。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。
select 5 % 2;
 1
 1
 1
 若对浮点数求余,可能会出现如下结果
select 4.2 % 2;
 1
 0.20000000000000018
 1
 注意:精度在 hive 中是个很大的问题,类似这样的操作最好通过 round 指定精度
select round(4.2 % 2, 2);
 1
 0.2
 1
 6、位与操作:&
 语法:A & B
 操作类型:整数类型
 说明:返回 A 和 B 按位进行与操作的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。
select 4 & 8;
 1
 0
 1
 select 6 & 4;
 1
 4
 1
 7、位或操作:|
 语法:A | B
 操作类型:整数类型
 说明:返回 A 和 B 按位进行或操作的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。
select 4 | 8;
 1
 12
 1
 select 6 | 8;
 1
 14
 1
 8、位异或操作:^
 语法:A ^ B
 操作类型:整数类型
 说明:返回 A 和 B 按位进行异或操作的结果。结果的数值类型等于 A 的类型和 B 的类型的最小父类型。
select 4 ^ 8;
 1
 12
 1
 select 6 ^ 4;
 1
 2
 1
 9、按位取反操作:~
 语法:~ A
 操作类型:整数类型
 说明:返回 A 按位取反操作的结果。结果的数值类型等于 A 的类型。
select ~ 4;
 1
 -5
 1
 select ~ 6;
 1
 -7
 1
  
逻辑运算
  
1、逻辑与操作:AND
 语法:A AND B
 操作类型:boolean
 说明:如果 A 或 B 为 NULL,则返回 NULL;如果 A 和 B 均为 TRUE,则为 TRUE;否则为 FALSE。
select 1 where 1 = 1 and 2 = 2;
 1
 1
 1
 2、逻辑与操作:OR
 语法:A OR B
 操作类型:boolean
 说明:如果 A 或 B 为 NULL,则返回 NULL;如果 A 或 B 有一个为 TRUE,则为 TRUE;否则为 FALSE。
select 1 where 1 = 2 or 2 = 2;
 1
 1
 1
 3、逻辑非操作:NOT
 语法:NOT A
 操作类型:boolean
 说明:如果 A 为 NULL,或者 A 为 FALSE,则为TRUE;否则为FALSE。
select 1 where not 1 = 2;
 1
 1
 1
  
条件运算
  
1、IF 函数:IF
 语法:IF(boolean testCondition, T valueTrue, T valueFalseOrNull)
 返回类型:T
 描述:当条件 testCondition 为 True 时,返回 valueTrue;否则返回 valueFalseOrNull。
select if(1 = 2, 100, 200);
 1
 200
 1
 2、非空查找函数:COALESCE
 语法: COALESCE(T v1, T v2, …)
 返回类型:T
 描述:返回参数中的第一个非空值;如果所有值都为 NULL,那么返回NULL。
select coalesce(null, '100', '50');
 1
 100
 1
 3、条件判断函数:CASE
 语法: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
 返回类型:T
 描述:如果 a 等于 b,那么返回 c;如果 a 等于 d,那么返回 e;否则返回 f。
select case 100 when 50 then 'tom' when 100 then 'jack' else 'mary' end 
 1
 jack
 1
 4、条件判断函数:CASE
 语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
 返回类型:T
 描述:如果 a 为 TRUE,则返回 b;如果 c 为 TRUE,则返回 d;否则返回 e。
select case when 1 = 2 then 'tom' when 2 = 2 then 'jack' else 'mary' end 
 1
 jack
 1
  
数值计算
  
1、近似函数:round
 语法:round(double a)
 返回类型:bigint
 说明:返回 double 类型的整数值部分 (遵循四舍五入)
select round(3.1415926);
 1
 3
 1
 select round(3.5);
 1
 4
 1
 2、指定精度近似函数:round
 语法:round(double a)
 返回类型:double
 说明:返回指定精度 d 的 double 类型
select round(3.1415926, 4);
 1
 3.1416
 1
 3、向下取整函数:floor
 语法:floor(double a)
 返回类型:double
 说明:返回不大于 a 的最大整数
select round(3.1415926, 4);
 1
 3
 1
 4、向上取整函数:ceil
 语法:ceil(double a)
 返回类型:bigint
 说明:返回不小于 a 的最小整数
select round(3.1415926, 4);
 1
 4
 1
 5、向上取整函数:ceiling
 语法:ceiling(double a)
 返回类型:bigint
 说明:与 ceil 功能相同
select round(3.1415926, 4);
 1
 4
 1
 6、取随机函数:rand
 语法:rand(),rand(int seed)
 返回类型:bigint
 说明:返回一个 0 到 1 范围内的随机数。如果指定种子 seed,则会得到一个稳定的随机数序列。
select rand();
 1
 0.712311371683693
 1
 select rand(100);
 1
 0.7308781907032909
 1
 7、自然指数函数:exp
 语法:exp(double a)
 返回类型:double
 说明:返回自然对数 e 的 a 次方
select exp(2);
 1
 7.38905609893065
 1
 8、以 10 为底的对数函数:log10
 语法:log10(double a)
 返回类型:double
 说明:返回以 10 为底的 a 的对数
select log10(100);
 1
 2.0
 1
 9、以 2 为底的对数函数:log2
 语法:log2(double a)
 返回类型:double
 说明:返回以 2 为底的 a 的对数
select log2(8);
 1
 3.0
 1
 10、对数函数:log
 语法:log2(double base, double a)
 返回类型:double
 说明:返回以 base 为底的 a 的对数
select log(2, 1024);
 1
 10.0
 1
 11、幂运算函数:pow
 语法:pow(double a, double p)
 返回类型:double
 说明:返回以 a 的 p 次幂
select pow(2, 5);
 1
 32.0
 1
 12、幂运算函数:power
 语法:power(double a, double p)
 返回类型:double
 说明:返回以 a 的 p 次幂,与 pow 功能相同。
select pow(2, 10);
 1
 1024.0
 1
 13、平方根函数:sqrt
 语法:sqrt(double a)
 返回类型:double
 说明:返回 a 的 平方根
select sqrt(100);
 1
 10.0
 1
 14、二进制函数:bin
 语法:bin(bigint a)
 返回类型:string
 说明:返回 a 的 二进制代码表示
select bin(7);
 1
 111
 1
 15、十六进制函数:hex
 语法:hex(bigint a)
 返回类型:string
 说明:如果变量是 int 类型,那么返回 a 的十六进制表示;如果变量是 string 类型,则返回该字符串的十六进制表示。
select hex(17);
 1
 11
 1
 select hex('abc');
 1
 616263
 1
 16、反转十六进制函数:unhex
 语法:unhex(string a)
 返回类型:string
 说明:返回该十六进制数或字符串所反转的字符串
select unhex(616263);
 1
 abc
 1
 select unhex('616263');
 1
 abc
 1
 17、进制转换函数:conv
 语法:conv(bigint num, int from_base, int to_base)
 返回类型:string
 说明:将数值 num 从 from_base 进制转化到 to_base 进制
select conv(17, 10, 2);
 1
 10001
 1
 select conv(17, 10, 16);
 1
 11
 1
 18、绝对值函数:abs
 语法:abs(int a ),absdouble a)
 返回类型:int double
 说明:返回数值 a 的绝对值
select abs(-1);
 1
 1
 1
 select abs(3.14);
 1
 3.14
 1
 19、正取余函数:pmod
 语法:pmod(int a, int b),pmod(double a, double b)
 返回类型:int double
 说明:返回正的 a 除以 b 的余数
select pmod(9, 4);
 1
 1
 1
 select pmod(-9, 4);
 1
 3
 1
 20、正弦函数:sin
 语法:sin(double a)
 返回类型:double
 说明:返回 a 的正弦值
select sin(0.8);
 1
 0.7173560908995228
 1
 21、反正弦函数:asin
 语法:asin(double a)
 返回类型:double
 说明:返回 a 的反正弦值
select sin(0.7173560908995228);
 1
 0.8
 1
 22、余弦函数:cos
 语法:cos(double a)
 返回类型:double
 说明:返回 a 的余弦值
select cos(0.8);
 1
 0.6967067093471654
 1
 23、反余弦函数:acos
 语法:acos(double a)
 返回类型:double
 说明:返回 a 的反余弦值
select acos(0.6967067093471654);
 1
 0.8
 1
 24、positive 函数:positive
 语法:positive(int a),positive(double a)
 返回类型:int double
 说明:返回 a 自身
select positive(-1);
 1
 -1
 1
 select positive(3.14);
 1
 3.14
 1
 25、negative 函数:negative
 语法:negative(int a),negative(double a)
 返回类型:int double
 说明:返回 a 的相反数
select negative(-1);
 1
 1
 1
 select negative(3.14);
 1
 -3.14
 1
  
日期函数
 1、UNIX 时间戳转日期函数:fom_unixtime
  
 语法:fom_unixtime(bigint unixtime[, string format])
 返回类型:string
 说明:转化 UNIX 时间戳(从1970-01-01 00:00:00 UTC 到指定时间的秒数)到当前时区的时间格式。
select from_unixtime(1590000000, 'yyyy-MM-dd');
 1
 2020-05-21
 1
 2、获取当前 UNIX 时间戳函数:unix_timestamp
  
 语法:unix_timestamp()
 返回类型:bigint
 说明:转获取当前时区的 UNIX 时间戳
select unix_timestamp();
 1
 1592361030
 1
 3、日期转 UNIX 时间戳函数:unix_timestamp
  
 语法:unix_timestamp(string date)
 返回类型:bigint
 说明:转换格式为 “yyyy-MM-dd HH:mm:ss” 的日期到 UNIX 时间戳。如果转换失败,则返回 NULL。
select unix_timestamp('2020-05-21 02:40:00');
 1
 1590000000
 1
 4、指定格式日期转 UNIX 时间戳函数:unix_timestamp
  
 语法:unix_timestamp(string date, string pattern)
 返回类型:bigint
 说明:转换 pattern 格式的日期到 UNIX 时间戳。如果转换失败,则返回 NULL。
select unix_timestamp('20200521 02:40:00', 'yyyyMMdd HH:mm:ss');
 1
 1590000000
 1
 5、日期时间转日期函数:to_date
  
 语法:to_date(string timestamp)
 返回类型:string
 说明:返回日期时间字段中的日期部分。
select to_date('2020-05-21 02:40:00');
 1
 2020-05-21
 1
 6、日期转年函数:year
  
 语法:year(string date)
 返回类型:int
 说明:返回日期中的年份。
select year('2020-05-21 02:40:00');
 1
 2020
 1
 7、日期转月函数:month
  
 语法:month(string date)
 返回类型:int
 说明:返回日期中的月份。
select month('2020-05-21 02:40:00');
 1
 5
 1
 8、日期转天函数:day
  
 语法:day(string date)
 返回类型:int
 说明:返回日期中的天。
select day('2020-05-21 02:40:00');
 1
 21
 1
 9、日期转小时函数:hour
  
 语法:hour(string date)
 返回类型:int
 说明:返回日期中的小时。
select hour('2020-05-21 02:40:00');
 1
 2
 1
 10、日期转分钟函数:minute
  
 语法:minute(string date)
 返回类型:int
 说明:返回日期中的分钟。
select minute('2020-05-21 02:40:00');
 1
 40
 1
 11、日期转秒函数:second
  
 语法:second(string date)
 返回类型:int
 说明:返回日期中的秒。
select second('2020-05-21 02:40:00');
 1
 0
 1
 12、日期转周函数:weekofyear
  
 语法:weekofyear(string date)
 返回类型:int
 说明:返回日期在该年的周数。
select weekofyear('2020-05-21 02:40:00');
 1
 21
 1
 13、日期比较函数:datediff
  
 语法:datediff(string enddate, string startdate)
 返回类型:int
 说明:返回结束日期减去开始日期的天数。
select datediff('2020-05-21', '2020-01-01');
 1
 141
 1
 14、日期增加函数:date_add
  
 语法:date_add(string startdate, int days)
 返回类型:string
 说明:返回开始日期 startdate 增加 days 天后的日期。
select date_add('2020-05-21', 10);
 1
 2020-05-31
 1
 15、日期减少函数:date_sub
  
 语法:date_sub(string startdate, int days)
 返回类型:string
 说明:返回开始日期 startdate 减少 days 天后的日期。
select date_sub('2020-05-21', 10);
 1
 2020-05-11
 1
  
字符串函数
  
1、字符串长度函数:length
 语法:length(string A)
 操作类型:int
 描述:返回字符串 A 的长度
select length('abcedfg');
 1
 7
 1
 2、字符串反转函数:reverse
 语法:reverse(string A)
 操作类型:string
 描述:返回字符串 A 的反转结果
select reverse('abcedfg');
 1
 gfdecba
 1
 3、字符串连接函数:concat
 语法:concat(string A, string B…)
 操作类型:string
 描述:返回输入字符串连接后的结果,支持任意多个输入字符串。
select concat(‘abc’, 'def’, 'gh');
 1
 abcedfg
 1
 4、带分隔符字符串连接函数:concat_ws
 语法:concat_ws(string SEP, string A, string B…)
 操作类型:string
 描述:返回输入字符串连接后的结果,SEP 表示各个字符串间的分隔符。
select concat_ws(',', 'abc', 'def', 'gh');
 1
 abc,def,gh
 1
 5、字符串截取函数:substr | substring
 语法:substr(string A, int start)
 操作类型:string
 描述:返回字符串 A 从 start 位置到结尾的字符串。
select substr('abcde', 3);
 1
 cde
 1
 select substring('abcde', -1);
 1
 e
 1
 6、字符串截取函数:substr | substring
 语法:substr(string A, int start, int len)
 操作类型:string
 描述:返回字符串 A 从 start 位置开始,长度为 len 的字符串。
select substr('abcde', 3, 2);
 1
 cd
 1
 select substring('abcde', -2, 2);
 1
 de
 1
 7、字符串转大写函数:upper | ucase
 语法:upper(string A)
 操作类型:string
 描述:返回字符串 A 的大写格式
select upper('Apple');
 1
 APPLE
 1
 8、字符串转小写函数:lower | lcase
 语法:lower(string A)
 操作类型:string
 描述:返回字符串 A 的小写格式
select upper('Apple');
 1
 apple
 1
 9、去空格函数:trim
 语法:trim(string A)
 操作类型:string
 描述:去除字符串两边的空格
select trim(' abc ');
 1
 abc
 1
 10、左边去空格函数:ltrim
 语法:ltrim(string A)
 操作类型:string
 描述:去除字符串左边的空格
select ltrim(' abc ');
 1
 abc[空格]
 1
 11、右边去空格函数:rtrim
 语法:rtrim(string A)
 操作类型:string
 描述:去除字符串右边的空格
select rtrim(' abc ');
 1
 [空格]abc
 1
 12、正则表达式替换函数:regexp_replace
 语法:regexp_replace(string A, string B, string C)
 操作类型:string
 描述:将字符串 A 中的符合 Java 正则表达式 B 的部分替换为 C
select regexp_replace('foobar', 'oo|ar', '');
 1
 fb
 1
 13、正则表达式解析函数:regexp_extract
 语法:regexp_extract(string subject, string pattern, int index)
 操作类型:string
 描述:将字符串 subject 按照 pattern 正则表达式的规则拆分,返回 index 指定的字符。
select regexp_extract('foothebar', 'foo(.*?)(bar)', 0);
 1
 foothebar
 1
 select regexp_extract('foothebar', 'foo(.*?)(bar)', 1);
 1
 the
 1
 select regexp_extract('foothebar', 'foo(.*?)(bar)', 2);
 1
 bar
 1
 14、URL 解析函数:parse_url
 语法:parse_url(string urlString, string partToExtract [, string keyToExtract])
 操作类型:string
 描述:返回 URL 中指定的部分。partToExtract 的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
select parse_url('https://www.baidu.com/path/p.php?k1=v1&k2=v2#ref1', 'HOST');
 1
 www.baidu.com
 1
 select parse_url('https://www.baidu.com/path/p.php?k1=v1&k2=v2#ref1', 'QUERY', 'k1');
 1
 v1
 1
 15、JSON 解析函数:get_json_object
 语法:get_json_object(string json_string, string path)
 操作类型:string
 描述:解析 json 的字符串 json_string,返回 path 指定的内容。如果输入的 json 字符串无效,那么返回 NULL。
select get_json_object('{"name":"zs","age":"25"}', '$.name');
 1
 zs
 1
 16、空字符串函数:space
 语法:space(int n)
 操作类型:string
 描述:返回长度为 n 的字符串
select space(3);
 1
 [空格][空格][空格]
 1
 17、重复字符串函数:repeat
 语法:repeat(string str, int n)
 操作类型:string
 描述:返回重复 n 次后的 str 字符串
select repeat('abc', 3);
 1
 abcabcabc
 1
 18、首字符 ASCII 函数:ascii
 语法:ascii(string str)
 操作类型:int
 描述:返回字符串 str 第一个字符的 ascii 码
select ascii('abc');
 1
 97
 1
 19、左补足函数:lpad
 语法:lpad(string str, int len, string pad)
 操作类型:string
 描述:将 str 用 pad 进行左补足到 len 位
select lpad('abc', 10, '#');
 1
 #######abc
 1
 20、右补足函数:rpad
 语法:rpad(string str, int len, string pad)
 操作类型:string
 描述:将 str 用 pad 进行右补足到 len 位
select rpad('abc', 10, '#');
 1
 abc#######
 1
 21、分割字符串函数:split
 语法:split(string str, string pat)
 操作类型:array
 描述:按照 pat 字符串分割 str,会返回分割后的字符串数组。
select split('ab,cd,ef', ',');
 1
 ["ab","cd","ef"]
 1
 22、集合查找函数:find_in_set
 语法:find_in_set(string str, string strList)
 操作类型:int
 描述:返回 str 在 strlist 第一次出现的位置,strlist 是用逗号分割的字符串。如果没有找到该 str 字符,则返回 0。
select find_in_set('cd','{ab,cd,ef}');
 1
 2
 1
  
聚合函数
  
 创建 test 表
create table test(id int, t int);
 1
 插入测试数据
insert into test values(1,60),(2,30),(3,80),(4,40),(5,60),(6,50),(7,80),(8,70),(9,90),(10,100);
 1
 查看表中数据
select * from test;
 1
 +----------+---------+
 | test.id  | test.t  |
 +----------+---------+
 | 1        | 60      |
 | 2        | 30      |
 | 3        | 80      |
 | 4        | 40      |
 | 5        | 60      |
 | 6        | 50      |
 | 7        | 80      |
 | 8        | 70      |
 | 9        | 90      |
 | 10       | 100     |
 +----------+---------+
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 1、统计函数:count
 语法:count(),count(expr),count(DISTINCT expr[, expr_.])
 返回类型:int
 描述:count() 统计检索出的行的个数,包括 NULL 值的行;count(expr) 返回指定字段的非空值的个数;count(DISTINCT expr[, expr_.]) 返回指定字段的不同的非空值的个数。
select count(*) from test;
 1
 10
 1
 select count(distinct t) from test;
 1
 8
 1
 2、求和函数:sum
 语法:sum(col),sum(DISTINCT col)
 返回类型:double
 描述:sum(col) 统计结果集中 col 的相加的结果;sum(DISTINCT col) 统计结果中 col 不同值相加的结果。
select sum(t) from test;
 1
 660
 1
 select sum(distinct t) from test;
 1
 520
 1
 3、平均值函数:avg
 语法:avg(col),avg(DISTINCT col)
 返回类型:double
 描述:avg(col) 统计结果集中 col 的平均值;avg(DISTINCT col) 统计结果中 col 不同值相加的平均值。
select avg(t) from test;
 1
 66.0
 1
 select avg(distinct t) from test;
 1
 65。0
 1
 4、最小值函数:min
 语法:min(col)
 返回类型:double
 描述:统计结果集中 col 字段的最小值
select min(t) from test;
 1
 30
 1
 5、最大值函数:max
 语法:max(col)
 返回类型:double
 描述:统计结果集中 col 字段的最大值
select max(t) from test;
 1
 100
 1
 6、总体方差函数:var_pop
 语法:var_pop(col)
 返回类型:double
 描述:统计结果集中 col 非空集合的总体变量(忽略 null)
select round(var_pop(t), 2) from test;
 1
 444.0
 1
 7、样本方差函数:var_samp
 语法:var_samp(col)
 返回类型:double
 描述:统计结果集中 col 非空集合的样本变量(忽略 null)
select round(var_samp(t), 2) from test;
 1
 493.33
 1
 8、总体标准差函数:stddev_pop
 语法:stddev_pop(col)
 返回类型:double
 描述:该函数计算总体标准偏离,并返回总体变量的平方根,其返回值与 VAR_POP 函数的平方根相同。
select round(stddev_pop(t), 2) from test;
 1
 21.07
 1
 9、样本标准差函数:stddev_samp
 语法:stddev_samp(col)
 返回类型:double
 描述:该函数计算样本标准偏离
select round(stddev_samp(t), 2) from test;
 1
 22.21
 1
 10、百分位函数:percentile
 语法:percentile(BIGINT col, p)
 返回类型:double
 描述:求准确的 p 对应的百分位数,p 必须介于 0 和 1 之间,返回类型为 double,但是 col 字段目前只支持整数,不支持浮点数类型。
select percentile(t, 0.5) from test;
 1
 65.0
 1
 11、百分位函数:percentile
 语法: percentile(BIGINT col, array(p1 [, p2]…))
 返回类型:array
 描述:功能和上述类似,array 中可以输入多个百分位数,返回类型也为 array,其中为对应的百分位数。
select percentile(t, array(0.2, 0.3, 0.5)) from test;
 1
 [48.0,57.0,65.0]
 1
 12、近似百分位函数:percentile_approx
 语法:percentile_approx(BIGINT col, p [, B])
 返回类型:double
 描述:求近似的 p 对应的百分位数,p 必须介于 0 和 1 之间,返回类型为 double,col 字段支持浮点类型。参数 B 控制内存消耗的近似精度,B越大,结果的准确度越高。默认为10,000。当 col 字段中的 distinct 值的个数小于 B 时,结果为准确的百分位数。
select percentile_approx(t, 0.5, 5) from test;
 1
 58.888888888888886
 1
 13、近似百分位函数:percentile_approx
 语法:percentile_approx(BIGINT col, array(p1 [, p2]…) [, B])
 返回类型:double
 描述:功能和上述类似,array 中可以输入多个百分位数,返回类型也为 array,其中为对应的百分位数。
select percentile_approx(t, array(0.2, 0.3, 0.5), 5) from test;
 1
 [35.0,50.0,58.888888888888886]
 1
 14、直方图:histogram_numeric
 语法:histogram_numeric(col, b)
 返回类型:array<struct {‘x’,‘y’}>
 描述:以 b 为基准计算 col 的直方图信息,得到的结果可用于作图。
select histogram_numeric(t, 100) from test;
 1
 [{"x":30.0,"y":1.0},{"x":40.0,"y":1.0},{"x":50.0,"y":1.0},{"x":60.0,"y":2.0},{
  










