【问题标题】:hive get each month end date蜂巢获取每个月的结束日期
【发布时间】:2017-12-15 16:23:25
【问题描述】:

我想要每个月的最后一个日期,例如 1 月 31 日、2 月 28 日等。 我在下面尝试了 current_date 并且它可以工作,但是当我使用我的日期列时它返回 null:

SELECT datediff(CONCAT(y, '-', (m + 1), '-', '01'), CONCAT(y, '-', m, '-', '01')) FROM 
(SELECT
month(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') ) as m, 
year(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') ) as y, 
day(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') )
from table2 ) t

返回:

_c0
    NULL 

SELECT
month(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') ) as m, 
year(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') ) as y, 
day(from_unixtime(unix_timestamp(C_date, 'yyyyMMdd'),'yyyy-MM-dd') )
from table2) t

返回:

m   | y  | _c2|
3   |2017|  21|

提前致谢。

【问题讨论】:

  • select last_day(current_date) - 返回完整日期 2017-12-31 ,我只需要 31 所以除非我 substr 我不会得到 31
  • hive 中有日历实用程序,请尝试使用 hive UDF 函数

标签: hadoop hive


【解决方案1】:

使用last_day()day() 函数:

hive> select day(last_day(current_date)) ;
OK
31
Time taken: 0.079 seconds, Fetched: 1 row(s)

在应用last_day() 之前应用to_date() 转换您的列。

【讨论】:

    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2019-04-10
    • 2013-01-09
    相关资源
    最近更新 更多