简体   繁体   中英

SQL find average of time difference between two rows based on conditions

I have a use case which am trying to solve using sql query.

Query Engine is based on Presto 0.172, https://prestodb.io/

Lets say I have a data like this

+----------+------------+-------------+------+--------------------------+
| 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 |
+----------+------------+-------------+------+--------------------------+

I am trying to find avg time difference when a particular actiontype lets say type1 starts and end for a given uuid

ie group by UUID, actiontype and location

In some cases I can have multiple entries for same actiontype and actionstate and in that case I need to pic the MAX(lastupdatedtime)

something like

select AVG(date_diff( MAX(lastupdatedtime of start)) and MAX(lastupdatedtime of end)

in table datatable group by location, actiontype, uuid.

You can use conditional aggregation in subtraction.

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 isn't needed as there is only one result for a combination of group by columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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