在HiveSql中,我有一个yearmonth [yyyymm]列,我需要从中减去3个月 例如:如果yearmonth是201912,则所需的记录是201909
有人可以帮我获得所需的语法或脚本吗?
我尝试过addmonths,conv()和reg_extract 但没有效果
答案 0 :(得分:1)
add_months()
函数可用于日期。将yyyyMM转换为yyyy-MM-01
日期,应用add_months
并再次格式化为yyyyMM
:
with your_table as (select '201912' as yearmonth)
select date_format(add_months(concat_ws('-',substr(yearmonth,1,4),substr(yearmonth,5,2),'01'),-3),'yyyyMM') as yearmonth
from your_table;
结果:
201909