配置单元sql:提取最近12个月的行
有一列“month”,类型为string,其值如下:配置单元sql:提取最近12个月的行,sql,hive,hiveql,Sql,Hive,Hiveql,有一列“month”,类型为string,其值如下: +--------+ | month | +--------+ | 201507 | | 201803 | | 201602 | | 201709 | +--------+ 熟悉配置单元时间操作的人请告诉我如何根据月份字段提取最近12个月的行。我想这正是您想要的: where cast(substring(month, 1, 4) || '-' || substring(month, 5, 2) || '-01' as date) >
+--------+
| month |
+--------+
| 201507 |
| 201803 |
| 201602 |
| 201709 |
+--------+
熟悉配置单元时间操作的人请告诉我如何根据月份字段提取最近12个月的行。我想这正是您想要的:
where cast(substring(month, 1, 4) || '-' || substring(month, 5, 2) || '-01' as date) >= add_months(current_date, -12)
从Hive 4.0.0开始,
add_months
支持可选参数output_date_format
:add_months(字符串start_date,int num_months,output_date_format)
在Hive 4.0.0之前:
select month
from table
where month >= date_format(add_months(current_date, -12),'yyyyMM');
这个专栏也有一年的历史。我想你需要为此编写代码并提取结果,然后进行同样的处理。提前谢谢。我得到了你想要的,但语法不正确
select month
from table
where month >= date_format(add_months(current_date, -12),'yyyyMM');