如何使用周值配置单元获取月值

问题描述

我想使用星期号获取月份值。我将周数存储在具有年值的表中 如何查询数据库以使用该星期值获取月值。 我正在使用hiveQL。

示例:

201801(week) ------->201801(month)
201804(week) ------->201801(month)

解决方法

假设一周有整整7天。提取周数,乘以7,加到'year-01-01',然后从结果日期中提取月份,用0填充l以从1得到'01'并与年份连接:

with data_example as(
select stack(3,'201801','201804','201805') as yr_wk
)

select yr_wk,concat(substr(yr_wk,1,4),lpad(month(date_add(date(concat(substr(yr_wk,'-01-01')),int(substr(yr_wk,5,2))*7)),2,0)
                    ) yr_mth
 from data_example

结果:

yr_wk   yr_mth
201801  201801
201804  201801
201805  201802

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...