–当前时间:
SELECT CURRENT_DATE(); --2021-08-16
SELECT CURRENT_DATE(); --2021-08-16 10:03:01.853
–截取当前时间,例如:202108
SELECT SUBSTR(REGEXP_REPLACE(CAST(CURRENT_DATE AS STRING),‘-’,‘’),1,6);
substr(regexp_replace(add_months(‘
p
m
p
y
e
s
t
e
r
d
a
y
s
t
r
′
,
−
1
)
,
′
−
′
,
′
′
)
,
1
,
6
)
−
−
上
月
2021
−
09
−
08
,
s
u
b
s
t
r
(
r
e
g
e
x
p
r
e
p
l
a
c
e
(
′
p_mp_yesterday_str',-1),'-',''),1,6)--上月 2021-09-08 ,substr(regexp_replace('
pmpyesterdaystr′,−1),′−′,′′),1,6)−−上月2021−09−08,substr(regexpreplace(′p_mp_yesterday_str’,‘-’,‘’),1,6) --当月
–查询前面几行数据
SELECT * FROM tb LIMIT 1
–更新表结构
INVALIDATE METADATA tb
–更新表数据
refresh tb
–添加列
alter table tb add columns(columns_name TIMESTAMP COMMENT ‘注释’)
–查看表结构
desc tb
–建分区表
CREATE TABLE IF NOT EXISTS TB (
YEAR_MONTH STRING COMMENT ‘年月’
,CUST_NUM DOUBLE COMMENT ‘客户数’
,ETL_TIME timestamp COMMENT ‘ETL时间’
)
COMMENT ‘表名称’
PARTITIONED BY (part_dt string COMMENT ‘分区字段’)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\001’
STORED AS PARQUET TBLPROPERTIES (“parquet.compression”=“SNAPPY”);
–插入数据
insert into table new_tb partition(part_dt=‘202110’)
select year_month,PRD_CD
,CREATED_TIME
,etl_time
from tb where part_dt=‘202110’;
–删除分区
ALTER TABLE 表名 DROP PARTITION (part_dt=‘202110’);
–修改分区名称202110-1 改为202110
alter table tb partition(part_dt=‘202110-1’)
rename to partition(part_dt=‘202110’)
–修改列名
alter table 表名 change 原列名 新列名 string
–修改列注释
ALTER TABLE 表名 CHANGE 原列名 新列名 字段类型 COMMENT ‘新注释’;
–查询表创建语句
show create table 表名
–修改日期格式
FROM_UNIXTIME(UNIX_TIMESTAMP(CONCAT(YEAR_MONTH,‘03’),‘yyyyMMdd’),‘yyyy-MM-dd’)
from_unixtime(unix_timestamp(‘2022-01-01’),‘yyyyMM’)
–清空表
truncate table tb
–修改表字段类型
alter table tb column 原列名 新列名 timestamp
–两个日期间的月数
select floor(months_between(
from_unixtime(unix_timestamp(concat(‘202201’,‘01’),‘yyyymmdd’),‘yyyy-mm-dd’) --大时间
,concat(‘2021-01’,‘-01’)–小时间
))
select months_between(‘2021-02-11 15:21:40’,‘2022-02-11 15:21:40’)
select to_timestamp(‘2021-02-11’,‘yyyy-MM-dd HH:mm:ss.SSSSSS’)
select cast(concat(‘2022-01’,‘-01’) as timestamp)
select months_between(cast(concat(‘2022-01’,‘-01’) as timestamp)
,cast(concat(‘2021-01’,‘-01’) as timestamp))