【发布时间】:2021-05-06 14:02:57
【问题描述】:
我是 SQL 新手(在 hive 中),我试图计算每个匿名 ID 在第一个事件和最后一个事件之间花费的时间(以分钟为单位)。资源表的时间戳被格式化为字符串, 比如:“2020-12-24T09:47:17.775Z”。我尝试了两种方式:
1- 将列时间戳转换为 bigint 并计算与主表的差异。
select anonymousid, max(from_unixtime(cast('timestamp' as bigint)) - min(from_unixtime(cast('timestamp' as bigint)) from db1.formevent group by anonymousid
在将其作为解决方案实施后,我得到了 NULL。
2- 从主资源创建一个新表,使用“where”设置要调用的条件,并尝试将“timestamp”转换为日期格式,而不进行任何最小-最大计算。
create table db1.successtime as select anonymousid, pagepath,buttontype, itemname, 'location', cast(to_date(from_unixtime(unix_timestamp('timestamp', "yyyy-MM-dd'T'HH:mm:ss.SSS"),'HH:mm:ss') as date) from db1.formevent where pagepath = "/account/sign-up/" and itemname = "Success" and 'location' = "Standard"
然后我又得到了 NULL,然后我离开了。 It looks like this
有什么方法可以重新格式化和计算第一个事件和最后一个事件('timestamp')之间的时间差以分钟为单位,并取按“位置”分组的平均值?
【问题讨论】:
-
看到这个答案:stackoverflow.com/a/57497316/2700344 除以 60 得到分钟
标签: sql hive casting timestamp hiveql