0
点赞
收藏
分享

微信扫一扫

Presto时间日常处理:

夏侯居坤叶叔尘 2022-01-06 阅读 94
大数据

--获取指定时间的上一个月份

select substr((date_format((cast(date(cast('2021-12-23 23:59:59' as timestamp)) - interval '1' month as timestamp)),'%Y-%m-%d')),1,7)

--获取当前时间的上一个月份:

select substr((date_format((cast(current_date - interval '1' month as timestamp)),'%Y-%m-%d')),1,7)

--获取当前时间:

select substr(cast(now() as varchar),1,19); #输出格式:2021-12-24 15:37:26

-- 数据类型转化

cast(字段名 as 数据类型)

-- 类似mysql的ifnull() 判空转化

coalesce(va.balance_amount,0)

-- 时间戳转年月日时分秒,

date_format()用法:

select date_format(cast('2021-10-11 10:56:11' as timestamp),'%Y-%m-%d');

format_datetime()用法:

format_datetime(cast(m.handle_time as timestamp),'HH:mm:ss') == mysql的date_format(m.handle_time),'HH:mm:ss')

/*获取当前月份*/

SELECT month(cast(now() as timestamp));

select CAST(DATE_ADD('day',1-day(current_date),current_date)as varchar(20));/* 当月第一天 */

/* 取N天前的日期 */

select current_date - interval '30' day;

/* 取前N月的第一天、和最后一天 */

select date_format(cast(concat(substr('2021-01-11 00:00:00',1,7),'-01') as timestamp) - interval 'N' month,'%Y-%m-%d');

select date_format(cast(concat(substr('2021-01-11 00:00:00',1,7),'-01') as timestamp) - interval 'N' month + interval '1' month - interval '1' second ,'%Y-%m-%d %H:%i:%s');

/* 算两个日期时间之差,时间比较 */

for function date_diff. Expected:

date_diff(varchar(x), date, date) ,

date_diff(varchar(x), timestamp, timestamp) ,

date_diff(varchar(x), time, time) ,

date_diff(varchar(x), time with time zone, time with time zone) ,

date_diff(varchar(x), timestamp with time zone, timestamp with time zone)

date_diff('day',cast(s.buy_time as timestamp),cast(s.insurance_end_time as timestamp))

-- 前面是开始时间,后面是结束时间

-- presto分页查询

SELECT * FROM (

SELECT ROW_NUMBER() over(ORDER BY 1) as Row,T.* FROM (这里填入sql语句或者表名) as T) TT

WHERE TT.Row BETWEEN 1 AND 10000;

/* 字符串转整数int */

cast(字段 as int)

/* NVL以及NVL2函数 */

NVL( comm,0) 如果字段comm为空返回0,不为空返回comm

NVL2(表达式1,表达式2,表达式3) 如果1为空,返回值为3。如果1不为空,返回值为2

/* 计算、保留小数 */

select cast(1 as decimal(10,4))/cast(3 as decimal(10,4)) as rate ;

举报

相关推荐

0 条评论