简体   繁体   中英

How to calculate interval between records of one table in PostgreSQL?

Example of my data (table is sorted by createdAt column):

 id | eventType |         createdAt          
----+-----------+----------------------------
  1 | login     | 2020-03-23 04:05:33.839+00
  2 | login     | 2020-03-23 07:52:55.684+00
  3 | login     | 2020-03-23 11:16:01.243+00
  4 | login     | 2020-03-23 11:34:28.91+00
  5 | login     | 2020-03-23 12:18:21.451+00
  6 | login     | 2020-03-23 13:03:54.742+00
  7 | logout    | 2020-03-23 18:23:26.815+00
  8 | login     | 2020-03-23 20:36:21.972+00
  9 | logout    | 2020-03-23 20:59:58.917+00
 10 | login     | 2020-03-24 04:18:20.432+00
 11 | login     | 2020-03-24 05:44:47.278+00
 12 | logout    | 2020-03-24 10:45:15.463+00
 13 | logout    | 2020-03-24 15:21:55.65+00
 14 | logout    | 2020-03-24 15:48:31.247+00
 15 | login     | 2020-03-24 16:33:23.735+00
 16 | logout    | 2020-03-24 17:07:59.192+00
 17 | logout    | 2020-03-24 18:15:03.661+00
 18 | login     | 2020-03-24 18:29:22.68+00
 19 | logout    | 2020-03-25 01:25:48.345+00
 20 | logout    | 2020-03-25 03:33:26.909+00
 21 | login     | 2020-03-25 10:20:54.3+00

How to calculate interval between each pair of login-logout?

For example. Record with ID 1 and record with ID 7 are the first pair of login-logout. Record with ID 8 and record with ID 9 are the second pair of login-logout and so on.

Some of the logins/logouts may not have pair.

My PostgreSQL version is 11.

self-join the table like

   select t1.id, t2.id from mytable t1 
   left join mytable t2 
   on t1.id+1 = t2.id 
   and t1.eventType = t2.eventType
   where t2.id is null

to get the valid rows as

1
7
8
9
10
12

substract createdAt of consecutive rows in a second step.

For each login, you want the next logout. Use a cumulative max:

select sum(logout - login)
from (select createdAt as login,
             min(createdAt) filter (where eventType = 'logout') over (order by createdAt desc) as logout,
             lag(eventType) over (order by createdAt) as prev_eventType
      from t
     ) t
where eventType = 'login' and
      eventType is distinct from prev_eventType

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