Hive的时间操作函数

目录

前言

hive 里面的时间函数有很多,今天单讲dayofweek函数,背景:有时候不仅要出日报,还要出周报,需要很多天归为同一周,求sum或者均值;

函数使用介绍

在Hive中,可以使用dayofweek()函数来计算某个日期是一周的第几天。该函数的语法如下:

sql 复制代码
dayofweek(date)

其中,date表示要计算的日期,可以是一个日期字符串或者一个日期类型的列名。

dayofweek()函数返回一个整数,表示日期所在周的第几天,返回值范围为1-7,其中1表示周日,2表示周一,以此类推。

例如,要计算当前日期是一周的第几天,可以使用以下HiveQL语句:

sql 复制代码
SELECT dayofweek(current_date());

该语句会返回当前日期所在周的第几天,例如,如果当前日期是周三,则返回值为4。

实际使用

判断该天是星期几

sql 复制代码
select 
case when dayofweek('${etl_date}') =1 then '星期日'
     when dayofweek('${etl_date}') =7 then '星期六'
     when dayofweek('${etl_date}') =6 then '星期五'
     when dayofweek('${etl_date}') =5 then '星期四'
     when dayofweek('${etl_date}') =4 then '星期三'
     when dayofweek('${etl_date}') =3 then '星期二'
     when dayofweek('${etl_date}') =2 then '星期一'
     else null 
     end as weekday

判断该天对应的周(包含一周开始和结束)

sql 复制代码
select 
case when dayofweek('${etl_date}') =1 then concat(date_add('${etl_date}',-6),'~','${etl_date}')
     when dayofweek('${etl_date}') =7 then concat(date_add('${etl_date}',-5),'~',date_add('${etl_date}',1))
     when dayofweek('${etl_date}') =6 then concat(date_add('${etl_date}',-4),'~',date_add('${etl_date}',2))
     when dayofweek('${etl_date}') =5 then concat(date_add('${etl_date}',-3),'~',date_add('${etl_date}',3))
     when dayofweek('${etl_date}') =4 then concat(date_add('${etl_date}',-2),'~',date_add('${etl_date}',4))
     when dayofweek('${etl_date}') =3 then concat(date_add('${etl_date}',-1),'~',date_add('${etl_date}',5))
     when dayofweek('${etl_date}') =2 then concat('${etl_date}','~',date_add('${etl_date}',6))
     else null 
     end as week_name
相关推荐
元媛媛10 小时前
数据仓库概要
数据仓库
cg.family10 小时前
Doris 数据仓库例子
数据仓库·doris
TDengine (老段)10 小时前
从 ETL 到 Agentic AI:工业数据管理变革与 TDengine IDMP 的治理之道
数据库·数据仓库·人工智能·物联网·时序数据库·etl·tdengine
镜舟科技1 天前
告别 Hadoop,拥抱 StarRocks!政采云数据平台升级之路
大数据·starrocks·数据仓库·hadoop·存算分离
程序猿 董班长1 天前
springboot配置多数据源(mysql、hive)
hive·spring boot·mysql
XueminXu2 天前
Hive使用Tez引擎出现OOM的解决方法
hive·oom·tez·datagrip·container·outofmemory
YF云飞2 天前
数据仓库进化:Agent驱动数智化新范式
数据仓库·人工智能·ai
KANGBboy5 天前
hive udf 执行一次调用多次问题
数据仓库·hive·hadoop
来杯雀巢咖啡6 天前
ubantu安装配置hive
数据仓库·hive·hadoop