【问题标题】:Cannot retrieve correct max date from Hive table?无法从 Hive 表中检索正确的最大日期?
【发布时间】:2018-02-05 12:25:19
【问题描述】:

我有点困惑。当我使用max(send_date) 时,我得到2018-02-04 23:59:51.0。但是,当我使用max(TO_DATE(from_unixtime(UNIX_TIMESTAMP(send_date, 'yyyy-mm-dd'))))TO_DATE(from_unixtime(max(UNIX_TIMESTAMP(send_date, 'yyyy-mm-dd')))) 时,我得到2018-01-31!!

为什么?

select max(send_date) from mytable;

Aso,当我使用 WHERE TO_DATE(from_unixtime(UNIX_TIMESTAMP(send_date, 'yyyy-mm-dd'))) = '2018-02-04' 时,我得到了结果 0,但这不是真的。

【问题讨论】:

  • send_date的数据类型是什么?我猜是一个字符串。
  • @GordonLinoff:就我使用describe mytable 所见,它是string
  • @GordonLinoff:也许我应该将yyyy-mm-dd 调整为包括小时、分钟、秒在内的实际输入格式?
  • 您只需要比较yyyy-mm-dd 还是需要包括小时、分钟、秒等?
  • @Bala:我发现了我的问题,我不得不使用yyyy-MM-dd 而不是yyyy-mm-dd:)))

标签: sql date hive hiveql


【解决方案1】:

我使用了不正确的转换格式。特别是,我使用了yyyy-mm-dd 而不是yyyy-MM-dd

更多细节可以在这里找到: http://bigdataprogrammers.com/string-date-conversion-hive/

【讨论】:

    【解决方案2】:

    to_date 与 Hive 一起使用时,您甚至不必指定该格式,因为默认情况下它会返回该格式。这是一个例子

      select max(to_date(d1)), max(d1), min(to_date(d1)), min(d1) from (
        select '2018-02-04 23:59:51.0' as d1
        union all
        select '2018-02-04 23:59:59.0' as d1
        union all
        select '2018-01-31 23:59:51.0' as d1
        union all
        select '2018-01-31 23:59:59.0' as d1
      ) tbl 
    

    输出

    OK
    2018-02-04  2018-02-04 23:59:59.0   2018-01-31  2018-01-31 23:59:51.0
    Time taken: 27.547 seconds, Fetched: 1 row(s)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多