Date 将3个月减去yyyymm月格式列配置单元sql

Date 将3个月减去yyyymm月格式列配置单元sql,date,hive,bigdata,hiveql,Date,Hive,Bigdata,Hiveql,在HiveSql中,我有一个yearmonth[yyyyymm]列,需要从中减去3个月 例如:如果yearmonth为201912,则需要的记录为201909 有人能帮我弄一下语法或脚本吗 我尝试了addmonths、conv()和reg_extract 但是什么都不起作用add_months()函数对日期起作用。将yyyyMM转换为日期,再次应用添加月份,格式为 with your_table as (select '201912' as yearmonth) select date_for

在HiveSql中,我有一个yearmonth[yyyyymm]列,需要从中减去3个月 例如:如果yearmonth为201912,则需要的记录为201909

有人能帮我弄一下语法或脚本吗

我尝试了addmonths、conv()和reg_extract 但是什么都不起作用

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