Aco
Aco

Reputation: 35

Groupings on SQL Timestamp Difference Query

I inquired about this earlier, but wanted to post a new question with further details after receiving some feedback. I'm currently using the following query to find the time between records using my Date field (Datetime format).

SELECT Call_Created_By, Date,
       timestampdiff(minute, lag(Date, 1) OVER (ORDER BY Date), date) as 'Time Between Calls (min)'
FROM visits_master
ORDER BY Date;  

This yields the following results-

  +-----------------+---------------------+--------------------------+
    | Call_Created_By | Date                | Time Between Calls (min) |
    +-----------------+---------------------+--------------------------+
    | User 1          | 2020-01-02 13:30:03 | 0                        |
    +-----------------+---------------------+--------------------------+
    | User 2          | 2020-01-02 13:33:41 | 3                        |
    +-----------------+---------------------+--------------------------+
    | User 1          | 2020-01-02 13:38:43 | 5                        |
    +-----------------+---------------------+--------------------------+
    | User 3          | 2020-01-02 13:38:49 | 0                        |
    +-----------------+---------------------+--------------------------+

While the query is functioning to find the difference between timestamps, I also want to sort it by the Call_Created_By field to ultimately look at the time between timestamps per user. My desired results are as follows-

+-----------------+---------------------+--------------------------+
| Call_Created_By | Date                | Time Between Calls (min) |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 13:30:03 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 13:38:43 | 8                        |
+-----------------+---------------------+--------------------------+
| User 2          | 2020-01-02 13:33:41 | 0                        |
+-----------------+---------------------+--------------------------+
| User 3          | 2020-01-02 13:38:49 | 0                        |
+-----------------+---------------------+--------------------------+

Thanks for your help.

Update- when trying this following query as suggested below

SELECT Call_Created_By, Date,
       timestampdiff(minute, lag(Date, 1) OVER (partition by Call_Created_By ORDER BY Date), date) as 'Time Between Calls (min)'
  FROM visits_master
ORDER BY Call_Created_By,Date;

I get the following results. You will see the time difference calculation does not function.

+-----------------+---------------------+--------------------------+
| Call_Created_By | Date                | Time Between Calls (min) |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:01:15 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:27:30 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:27:30 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:27:30 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:27:30 | 26                       |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:27:30 | 0                        |
+-----------------+---------------------+--------------------------+
| User 1          | 2020-01-02 15:42:57 | 15                       |
+-----------------+---------------------+--------------------------+

Upvotes: 0

Views: 57

Answers (2)

George Joseph
George Joseph

Reputation: 5922

Updated query if you want to see by day the diff in timestamp records

select x.Call_Created_By
       ,x.date1
       ,max(time_between_calls) over(partition by call_created_by,grp) as time_between_calls_2
   from(
SELECT Call_Created_By
      ,Date1
      ,timestampdiff(minute, lag(Date1, 1) OVER (partition by Call_Created_By,cast(date1 as date) ORDER BY Date1), date1) as time_between_calls
      ,count(*) over(order by date1) as grp
  FROM t
)x
ORDER BY x.Call_Created_By
        ,x.Date1

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b60695dcdc734d15a8b395125f47633

Query to consider cases where the timestamp of call is same as that of the previous record.

select x.Call_Created_By
       ,x.date1
       ,max(time_between_calls) over(partition by call_created_by,grp) as time_between_calls_2
   from(SELECT Call_Created_By
              ,Date1
              ,timestampdiff(minute, lag(Date1, 1) OVER (partition by Call_Created_By ORDER BY Date1), date1) as time_between_calls
              ,count(*) over(order by date1) as grp
         FROM t
        )x
ORDER BY x.Call_Created_By
        ,x.Date1

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=04788f49c032ae1ae0eca218a1b3bd9a

The change you would need is to partition by "Call_Created_By" field

SELECT Call_Created_By, Date,
       timestampdiff(minute, lag(Date, 1) OVER (partition by Call_Created_By ORDER BY Date), date) as 'Time Between Calls (min)'
  FROM visits_master
ORDER BY Call_Created_By,Date;

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269963

In addition to the partition by, if you want zero values, use the three-argument form of lag:

SELECT Call_Created_By, Date,
       timestampdiff(minute,
                     lag(Date, 1, date) OVER (PARTITION BY call_created_by ORDER BY Date),
                     date
                   ) as minutes_between_calls
FROM visits_master
ORDER BY Date;  

Upvotes: 0

Related Questions