SQL根据条件查找两行之间时间差的平均值
我有一个使用sql查询解决的用例 查询引擎基于Presto 0.172 假设我有这样的数据SQL根据条件查找两行之间时间差的平均值,sql,presto,Sql,Presto,我有一个使用sql查询解决的用例 查询引擎基于Presto 0.172 假设我有这样的数据 +----------+------------+-------------+------+--------------------------+ | location | actiontype | actionstate | uuid | lastupdatedtime | +----------+------------+-------------+------+----------
+----------+------------+-------------+------+--------------------------+
| location | actiontype | actionstate | uuid | lastupdatedtime |
+----------+------------+-------------+------+--------------------------+
| x | type1 | start | 123 | 2018-09-09T16:54:37.648Z |
| x | type1 | start | 123 | 2018-09-09T16:55:37.648Z |
| x | type1 | start | 123 | 2018-09-09T16:56:37.648Z |
| x | type1 | end | 123 | 2018-09-09T16:57:37.648Z |
| x | type1 | end | 123 | 2018-09-09T16:58:37.648Z |
| y | type1 | start | 567 | 2018-09-09T14:57:37.648Z |
| y | type1 | end | 567 | 2018-09-09T14:58:37.648Z |
+----------+------------+-------------+------+--------------------------+
我试图找到一个特定的actiontype(比如type1)在给定uuid开始和结束时的平均时间差
i、 e.按UUID、操作类型和位置分组
在某些情况下,对于相同的actiontype和actionstate,我可以有多个条目,在这种情况下,我需要pic最大值(LastUpdateTime)
差不多
select AVG(date_diff( MAX(lastupdatedtime of start)) and MAX(lastupdatedtime of end)
表内数据表
按位置、操作类型、uuid分组。您可以在减法中使用条件聚合
select TIMEDIFF(MAX(case when actionstate='end' then lastupdatedtime end)
,MAX(case when actionstate='start' then lastupdatedtime end)
)
from datatable
where actionstate in ('start','end')
group by location, actiontype, uuid
having count(distinct actionstate) = 2
avg
不需要,因为group by列的组合只有一个结果。请标记正在使用的数据库。更新了查询引擎ANGENAKS Vamsi,我可以对上面的内容做一些小的修改,以获得我想要的内容,我不得不在prestodb查询格式中使用date_diff,而不是TIMEDIFF。唯一的问题是,上面的查询还拾取开始/结束未退出的行,并拾取最大值为空/空。有没有办法忽略actionstate既不匹配开始/结束也不匹配两者的元组。@Scorpion。。检查编辑,确保分组组合应有开始
和结束
行。我尝试了此操作,但看起来这不是一个有效的查询,从AWS Athena获取异常,错误代码为:InvalidRequestException;只要am添加具有计数的行(不同的actionstate)=2