【博学谷学习记录】超强总结,用心分享 | hive 日期函数与条件函数

【博学谷IT技术支持】

公众号: 积雷山摩云洞,欢迎关注!!!

操作

日期函数

  • 获取当前时间
-- 获取当前时间
-- 获取年月日
select `current_date`(); -- 2022-10-30
-- 获取年月日时分秒
select `current_timestamp`(); -- 2022-10-30 21:31:11.133000000
  • 获取当前UNIX时间戳函数
-- 获取当前UNIX时间戳函数
--- 获得当前时区的UNIX时间戳 (秒)
select unix_timestamp(); -- 1667136858
  • UNIX时间戳转日期函数:from_unixtime
-- UNIX时间戳转日期函数:from_unixtime
select from_unixtime(1667136858,"yyyy-MM-dd HH:mm:ss"); -- 2022-10-30 13:34:18
  • 日期转UNIX时间戳函数
-- 日期转UNIX时间戳函数
select unix_timestamp("2022-10-30 13:34:18"); -- 1667136858
  • 指定格式日期转UNIX时间戳函数
-- 指定格式日期转UNIX时间戳函数
select unix_timestamp('20221030 13:34:18','yyyyMMdd HH:mm:ss'); -- 1667136858
  • date_format函数
-- date_format函数
--- 格式化日期
select date_format('2022-1-1 1:1:1','yyyy-MM-dd HH:mm:ss'); ---2022-01-01 01:01:01
  • 日期时间转日期函数
-- 日期时间转日期函数
-- 返回日期时间字段中的日期部分
select to_date("2022-10-30 13:34:18"); --- 2022-10-30
  • 日期转年函数
-- 日期转年函数
--- 返回日期中的年
select year('2022-10-30 10:03:01'); -- 2022
  • 日期转年函数
-- 日期转年函数
-- 返回日期中的月份。
select month("2022-10-30 10:03:01"); -- 10
  • 日期转天函数
--- 日期转天函数
-- 返回日期中的天
select day("2022-10-30 10:03:01"); -- 30
select minute("2022-10-30 10:03:01"); -- 3
select second("2022-10-30 10:03:01"); -- 1
  • 日期转周函数
-- 日期转周函数
--- 返回日期在当前的周数
select weekofyear("2022-10-30 10:03:01"); -- 43
  • 日期比较函数
-- 日期比较函数
-- 返回结束日期减去开始日期的天数。
select datediff("2022-10-30","2022-11-1"); -- -2
  • 日期增加函数
-- 日期增加函数
-- 返回开始日期startdate增加days天后的日期
select date_add("2022-10-30",10); --- 2022-11-09
  • 日期减少函数
-- 日期减少函数
-- 返回开始日期startdate减少days天后的日期。
select date_sub("2022-10-30",10); -- 2022-10-20

条件函数

  • if函数
-- if函数
-- if(boolean testCondition, T valueTrue, T valueFalseOrNull)
-- 当条件testCondition为TRUE时,返回valueTrue;否则返回valueFalseOrNull
select if(1=2,'对了','错了');-- 错了
  • 条件判断函数:CASE
-- 条件判断函数:CASE
-- 说明:如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f
select case 1 when 2 then "参数与2相等" when 3 then "参数与3相等" else "不等于所有参数" end; -- 不等于所有参数

转化函数

  • 转化函数
-- 转化函数
-- cast(表达式 as 数据类型)
select cast(12.35 as int); -- 12
select cast('2020-12-05' as date); --- 2020-12-05

行转列

-- 行转列
-- 行转列是指多行数据转换为一个列的字段。
-- concat(str1,str2,...)  --字段或字符串拼接
-- concat_ws(sep, str1,str2) --以分隔符拼接每个字符串
-- collect_set(col)/collect_list(col) --将某字段的值进行去重汇总,产生array类型字段

-- 20   SMITH
-- 30  ALLEN
-- 30  WARD
-- 20   JONES
-- 30  MARTIN
-- 30  BLAKE
-- 10  CLARK
-- 20   SCOTT
-- 10  KING
-- 30   TURNER
-- 20   ADAMS
-- 30   JAMES
-- 20  FORD
-- 10   MILLER
drop table emp;
create table emp (
    deptno int,
    ename string
) row format delimited fields terminated by "\t";
load data local inpath "/export/data/hivedatas/emp.txt" into table emp;
select * from emp;

select deptno, concat_ws("-",collect_list(ename)) enames from emp group by deptno;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tMysvM8W-1670143585582)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/288e519dcf204c0fb6226fdec7cd53bf~tplv-k3u1fbpfcp-watermark.image?)]

posted @ 2023-06-01 09:59  牛牛牛牛牛牛牛牛  阅读(19)  评论(0编辑  收藏  举报