我想用周号得到月值。我把周数存储在一个带有年值的表中。如何查询数据库来用周值得到月值。我正在使用hiveql。示例:
201801(week) ------->201801(month) 201804(week) ------->201801(month)
tag5nh1u1#
假设一周有整整7天。提取周数,乘以7,相加到'year-01-01'并从结果日期提取月份,lpad使用0从1得到'01',并与year串联:
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, 1,4),'-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条答案
按热度按时间tag5nh1u1#
假设一周有整整7天。提取周数,乘以7,相加到'year-01-01'并从结果日期提取月份,lpad使用0从1得到'01',并与year串联:
结果: