SQL 如何使用 Hive 以分钟为单位获取日期差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33593080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:10:10  来源:igfitidea点击:

How to get date difference in minutes using Hive

sqlhadoophivedate-difference

提问by displayname

Below query is my sql server query and I want it to convert it into hive query:

下面的查询是我的 sql server 查询,我希望它将其转换为 hive 查询:

select DATEDIFF([minute], '19000101', '2013-01-01 10:10:10')

回答by Lukasz Szozda

You could use unix_timestampfor dates after 1970:

您可以在unix_timestamp以下日期之后使用1970

SELECT (unix_timestamp('2013-01-01 10:10:10') 
      - unix_timestamp('1970-01-01 00:00:00'))/60 
  1. Convert both dates to seconds from 1970-01-01
  2. Substract them
  3. Divide by 60 to get minutes
  1. 将两个日期转换为秒 1970-01-01
  2. 减去它们
  3. 除以 60 得到分钟

EDIT:

编辑:

Adding Minutes: change date to unixtime -> add var * 60sec -> convert back to date

添加分钟: change date to unixtime -> add var * 60sec -> convert back to date

SELECT from_unixtime(unix_timestamp('2013-01-01 10:10:10') + 10 * 60) AS result

db<>fiddle demo using MySQL

使用 MySQL 的 db<>fiddle 演示