I found a variety of previous topics that addressed this such as:
SQL Datediff - find datediff between rows SQL query to get DateDiff of the last two records
However, I cannot understand how this works. I am not advanced or an expert in SQL, so this is bothering me. I have created the following on fiddle:
CREATE TABLE activity
(
IDOrder int,
IDActivity int,
DateActivity DateTime
);
INSERT INTO activity (IDOrder, IDActivity, DateActivity) VALUES
(1, 1, "2007-04-16 08:34:00"),
(1, 2, "2007-04-16 09:22:00"),
(1, 3, "2007-04-16 09:51:00"),
(1, 4, "2007-04-16 16:14:00"),
(2, 1, "2007-04-16 08:34:00"),
(3, 1, "2007-04-16 08:34:00"),
(3, 2, "2007-04-16 09:22:00"),
(3, 3, "2007-04-16 09:51:00"),
(3, 4, "2007-04-16 16:14:00"),
(4, 1, "2007-04-16 08:34:00"),
(4, 2, "2007-04-16 09:22:00"),
(4, 3, "2007-04-16 09:51:00");
INSERT INTO activity (IDOrder, IDActivity , DateActivity) VALUES
(2, 2, CURRENT_TIMESTAMP),
(2, 3, CURRENT_TIMESTAMP);
Could someone give me a push in the right direction? What I want as output is 4 columns for these differences in minutes. Time 1 is the difference between IDActivity 1 and 2. Time 2 is the difference between IDActivity 2 and 3. Time 3 is between 3 and 4 and Time 4 is between 4 and 1. In some instances where the IDActivity doesn't exist, I would want it to return null.
The best idea I have is using an IF THEN statement, a giant loop returning 4 columns of the data, but am unsure how to implement it.