Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
配置单元sql:提取最近12个月的行_Sql_Hive_Hiveql - Fatal编程技术网

配置单元sql:提取最近12个月的行

配置单元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”,类型为string,其值如下:

+--------+
| 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');