Hive解决weekofyear跨年周异常问题


-- year_weeks
select 
year_weeks
,substr(calendar_date_a,1,10) calendar_date
from(
  select ceiling(dayofyear(calendar_date)/7) year_weeks,calendar_date_a
  from(
    select
    calendar_date,calendar_date calendar_date_a
    from csv_input 
    where -- substring(calendar_date,1,4) = 2021 and
    (calendar_date between concat(substring(calendar_date,1,4),'-01-01') and (date_add(concat(substring(calendar_date,1,4),'-01-01'), (7 - weekday(concat(substring(calendar_date,1,4),'-01-01')) - 1))))
  )
  union all
  select (weekofyear(calendar_date) + 1) year_weeks ,calendar_date_a
  from(
    select
    calendar_date,calendar_date calendar_date_a
    from csv_input 
    where --substring(calendar_date,1,4) = 2021 and
    (calendar_date between (date_add(concat(substring(calendar_date,1,4),'-01-01'), (7 - weekday(concat(substring(calendar_date,1,4),'-01-01')))))
    and concat(substring(calendar_date,1,4),'-12-31'))
  )
)
as year_weeks;

贴一下weekofyear执行2021-01-01效果

SELECT weekofyear('2021-01-01');

result:
#	weekofyear_CAST_2021-01-01_AS_DATE_
0	53

正常来讲,我们想要的2021-01-01这一天对应2021年的周数应该是第一周,但是执行的结果是第53周,原因是hive底层对于weekofyear函数的周数是按照这一周在上一年的天数多还是在这一年的天数多进行划分的。例如,

先贴处理逻辑,有时间再写

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值