【问题标题】:HIVE Obtain First Weekday of Current MonthHIVE 获取当月的第一个工作日
【发布时间】:2020-02-06 14:50:59
【问题描述】:

早上, 标题说明了一切。我一生都无法弄清楚如何在 HQL 中获取当月(或上月等)的第一个工作日。

因此,如果要评估今天的日期,它应该返回 2/3/2020 作为日期,因为 3 号是本月的第一个工作日。

我已经尝试用 case 语句来评估当月的第一天,如果是星期六,加 2 天,星期天,加 1,但它不起作用,我收到以下错误:错误:准备错误:org.apache.hive.service.cli.HiveSQLException:编译语句时出错:FAILED:ParseException 行 1:452 无关输入 ',' 期望 KW_THEN 靠近 ''line 1:626 无关输入 ',' 期望 KW_THEN 靠近 ''

case when date_format(date_add(current_date, 1 - day(current_date)),'u')=6, then  to_date(mydate) = date_add(date_add(current_date, 1 - day(current_date)+2))
        when date_format(date_add(current_date, 1 - day(current_date)),'u')=7, then  to_date(mydate) = date_add(date_add(current_date, 1 - day(current_date)+1))
            else to_date(mydate) = date_add(date_add(current_date, 1 - day(current_date))) end

请帮忙!

@Vamsi 普拉巴拉

我试过了,但是我收到的结果是 1。我需要返回一个日期,特别是本月的 2020 年 2 月 3 日。

case when date_format(date_add(current_date, 1 - day(current_date)),'u')> 5 then  
to_date(mydate)= to_date(date_add(date_add(current_date, 1 - day(current_date)),8-cast(date_format(date_add(current_date, 1 - day(current_date)),'u') as int)%8))
    else to_date(mydate) = date_add(current_date, 1 - day(current_date)) end

【问题讨论】:

  • 上一篇文章有​​帮助吗? stackoverflow.com/questions/22982904/…
  • 我已经能够通过以下内容确定它是星期几; when date_format(current_date,'u')=1 (1= monday, 2 = Tuesday etc.) 但是我不知道如何结合它来确定这是否是本月的第一个工作日。
  • 因此,如果要评估今天的日期,它应该返回 2/3/2020 作为日期,因为 3 号是本月的第一个工作日。这有意义吗?
  • 啊现在我明白了。对不起,我的错。不幸的是,我不知道如何在 Hive 中确定这一点。

标签: hive hiveql


【解决方案1】:

一个选项是获取月份的第一天,然后如果该月的第一天是周末,则添加天数。默认工作日1 = Monday, 2 = Tuesday ... 6 = Saturday, 7 = Sunday

select dt
      ,case when date_format(first_day_in_month,'u') > 5 
            then date_add(first_day_in_month,8-cast(date_format(first_day_in_month,'u') as int))
       else first_day_in_month end as first_week_day
from (select dt
            ,date_sub(dt,cast(date_format(dt,'d') as int)-1) as first_day_in_month
      from tbl 
     ) t 
;

请注意,date_format 函数适用于 Hive 1.2.0 及更高版本。

【讨论】:

  • 我在上面的原始帖子中尝试了类似的方法。我只是尝试了您建议的变体,但收到了一个新错误。如上所示。想法?
  • 我需要最终结果来返回日期
  • 凹凸。还有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多