【问题标题】:I need to make the difference between two timestamp fields, start and end我需要区分两个时间戳字段,开始和结束
【发布时间】:2020-03-24 06:18:46
【问题描述】:

我需要区分两个时间戳字段,开始和结束,例如: 开始日期:2019-09-20 01:15:00 final_date: 2019-09-20 01:20:15 datediff (end_date, start_date)

我得到的回报是 0。

我认为这是因为这一天是相同的,但我希望以分钟为单位,在这种情况下为 5 分钟。

有人遇到过这种困难吗?

【问题讨论】:

    标签: sql hive datediff


    【解决方案1】:

    一种方法是转换为 Unix 时间戳:

    select (unix_timestamp(final_date) - unix_timestamp(start_date)) / 60 as diff_minutes
    

    【讨论】:

      【解决方案2】:

      MySQL DATEDIFF() 返回两个日期或日期时间之间的天数。 要获得两个时间戳之间的秒数,您可以使用 UNIX_TIMESTAMP(date) 返回 unix 时间,所以这样做

      SELECT (UNIX_TIMESTAMP('2019-09-20 01:20:00')-UNIX_TIMESTAMP('2019-09-20 01:15:00'))/60;
      

      或使用TIMESTAMPDIFF

      SELECT TIMESTAMPDIFF(MINUTE,'2019-09-20 01:15:00','2019-09-20 01:20:00')
      

      【讨论】:

        猜你喜欢
        • 2019-06-08
        • 1970-01-01
        • 2021-01-26
        • 1970-01-01
        • 2020-02-22
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        相关资源
        最近更新 更多