简体   繁体   English

Presto SQL / Athena:如何仅返回小时、分钟和秒,从“间隔天到秒”

[英]Presto SQL / Athena: how to return only hours, minutes and seconds, from "interval day to second"

I have a query that from two timestamps calculates the difference and returns as a result a "interval day to second" like this:我有一个查询,从两个时间戳计算差异并返回“间隔天到秒”,如下所示:

SELECT (interval '1' second)*(timestamp_1 - timestamp_2) as time_delta

The result returned look like this:返回的结果如下所示:

  |  time_delta
--+----------------
  | 0 03:28:47.000
  | 0 02:20:37.000
  | 0 00:55:12.000

etc...

and the typeof() that result is "interval day to second".和 typeof() 结果是“间隔天到秒”。

Is there a way to keep only the hour-minute and second part?有没有办法只保留时分和秒部分? So that I would get something like:所以我会得到类似的东西:

  |  time_delta
--+----------------
  |   03:28:47
  |   02:20:37
  |   00:55:12

or, at the very least, remove the "day" part?或者,至少,删除“天”部分?

Thanks in advance.提前致谢。

Presto does not have functions to format interval values. Presto 没有格式化interval值的功能。 Your options:您的选择:

cast to varchar + regexp_replace : castvarchar + regexp_replace

presto:default> SELECT regexp_replace(CAST(parse_duration('3789s') AS varchar), '^0 (.*)\.000', '$1');
  _col0
----------
 01:03:09

or hour() , minute() and second() functions + concatenate.hour()minute()second()函数+连接。 This is greatly simplified by the format() function, but that's not available on Athena yet. format()函数极大地简化了这一点,但在 Athena 上尚不可用。

You should be able to use the presto convienience function for the timestamp that you get back.您应该能够使用 presto convienience 功能获取您返回的时间戳。 It looks like presto supports the MySQL function format so you should be able to use date_parse based on the presto docs.看起来 presto 支持 MySQL 函数格式,因此您应该能够使用基于 presto 文档的date_parse

Something like就像是

SELECT date_parse((interval '1' second)*(timestamp_1 - timestamp_2), %r) as time_delta

More info here: https://trino.io/docs/current/functions/datetime.html更多信息: https : //trino.io/docs/current/functions/datetime.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM