sql基础函数

小点笔记:

格式化数据类型:

cast(col as int)

日期、时间

时间戳-时间转化:

select FROM_UNIXTIME(1156219870)
--> 2006-08-22 12:11:10

时间-时间戳转化:

select UNIX_TIMESTAMP('2006-11-04 12:23:00');
-->1162614180

输出当前时间戳:

select UNIX_TIMESTAMP();  | select UNIX_TIMESTAMP(now());

时间格式化:

select DATE_FORMAT(FROM_UNIXTIME(1156219870),'yyyy-MM-dd HH:mm:ss')
-->2006-08-22 12:11:10

日期函数:

select trunc(CURRENT_DATE, 'MM'),  --当月第一天
 last_day(CURRENT_DATE);  --当月最后一天

取日期小时、分钟、秒:
[to_date、year、month、day、hour、minute、second]

select to_date('2011-12-08 10:03:01')
-->2011-12-08
select year('2011-12-08 10:03:01')
-->2011
select month('2011-12-08 10:03:01')
-->12
select day('2011-12-08 10:03:01')
-->8
select hour('2011-12-08 10:03:01')
-->10
select minute('2011-12-08 10:03:01')
-->3
select second('2011-12-08 10:03:01')
-->1

相差的天数:

select datediff('2012-12-08','2012-10-08');   
-->61

增加天数:

select date_add('2012-12-08',4);   
-->2012-12-12

减少天数:

select date_sub('2012-12-08',4);   
-->2012-12-04

时间差:
unix_timestamp() - unix_timestamp(ymdhms)是两个时间转换为timestamp之后相减,timestamp单位是秒,相减之后是两个时间之间相差的秒数。

CAST((unix_timestamp() - unix_timestamp(ymdhms)) % 60 AS int)是相差的秒数。
CAST((unix_timestamp() - unix_timestamp(ymdhms)) / 60 AS int) % 60是相差的分钟数。
CAST((unix_timestamp() - unix_timestamp(ymdhms)) / (60 * 60) AS int) % 24是相差的小时数。
CAST((unix_timestamp() - unix_timestamp(ymdhms)) / (60 * 60 * 24) AS int)是相差的天数。

eg: 小时

select CAST((unix_timestamp('2020-10-21 12:23:00') - unix_timestamp('2020-10-21 10:23:00'))/ (60 * 60) AS int) % 24
-->2

整型转时间类型:
步骤:先转时间为string,后转时间戳,最后转时间

set dt=20200701    --整型
select ${dt},
from_unixtime(unix_timestamp(cast(${dt} as string),'yyyyMMdd'),'yyyy-MM-dd') ;

判断条件:

1、if和case: 都是处理单个列的判断查询结果

select count(if(d2['01008'] is not null and d2['01008']!='' ,1,null)) IMSI,
case when分开:
select case os when 'android' then 'android' when 'ios' then 'iPhone' else 'PC' end as os,
case when一起:
select sum(case when before_prefr_unit_price < 100 then 1 else 0 end ) as a1,
       sum(case when before_prefr_unit_price >= 100 and before_prefr_unit_price < 200 then 1 else 0 end) as a2,
       sum(case when before_prefr_unit_price >= 200 and before_prefr_unit_price < 300 then 1 else 0 end) as a3,
       sum(case when before_prefr_unit_price >= 300 then 1 else 0 end) as a4
from gdm_m04_ord_det_sum
where  to_date(sale_ord_dt) = '2014-11-11' and dp = 'ACTIVE'

2、nvl对于null的判断:实现null值的替换
select nvl(col,1) from tb --如果第一个参数是null,输出用第二个替换
3、coalesce:非空查找函数 ,可以有很多个参数

select COALESCE(null,null,null,50);

substr截取字符串:

select substr('abcde',3)
-->cde
select substr('abcde',-3,2)  --2表示len
-->cd

split字符串分割:

select split('192.168.0.1','\\.')[0];
-->192

正则[like、rlike、regexp、regexp_replace、regexp_extract]

1、like [_表示单个字符、%标识多个字符]

select 'football' like '__otba%'
-->true

2、rlike与regexp 用法相同

"." 任意单个字符
"*" 匹配前面的字符0次或多次
"+" 匹配前面的字符1次或多次
"?" 匹配前面的字符0次或1次
"\d" 等于 [0-9],使用的时候写成’\d’
"\D" 等于 [^0-9],使用的时候写成’\D’

select 'football' rlike '^footba'
-->true
select 'does' rlike 'do(es)?'
-->true

3、regexp_replace:符合正则表达 进行替换

select regexp_replace('h234ney', '\\d+', 'o')
-->honey

4、regexp_extract:按照正则拆分,返回指定index字符 从1开始

select regexp_extract('honeymoon', 'hon(.*?)(moon)', 1);
-->honeymoon
select regexp_extract('honeymoon', 'hon(.*?)(moon)', 1);
-->ey
select regexp_extract('honeymoon', 'hon(.*?)(moon)', 2);
-->moon

贪婪匹配最后一个斜杠:

select
regexp_extract('com.baidu.searchbox/oat/arm/chinamobile/cactus.com.baidu.searchbox','.*/(.*)',1) c_0

数学函数:

1、round保留n位小数

select round(12.344,2)  
-->12.34

查看表的字段信息及元数据存储路径

desc formatted table_name;

动态分区参数设置

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

删除分区数据

alter table 表名 drop partition(stat_date=',,');

插入数据
insert overwrite 会覆盖已经存在的数据,
insert into 只是简单的插入,不考虑原始表的数据,直接追加到表中。

bash查询数据保存本地

hive -e 'select * from table_name;' >> /home/test.txt
hive -f exer.sql >> /home/test.txt

like用法

select * from user where name like concat(%, 字段,%)

创建表

CREATE TABLE IF NOT EXISTS tmp.code_name (
rule_name string,
rule_code string)
row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile;

join
join hive进行join没有不等量join 需要left join 后判空操作
即:

on b.noriskfactor=c.tag where c.tag is null 

下面错误

on b.noriskfactor=c.tag and c.tag is null 

你可能感兴趣的:(Hive,hive)