2

I have a table like : session is the name of the table for example With columns: Id, sessionDate, user_id

What i need: Delta should be a new calculated column

Id | sessionDate          | user_id | Delta in days
------------------------------------------------------
1  | 2011-02-20 00:00:00 |     2    | NULL
2  | 2011-03-21 00:00:00 |     2    | NULL
3  | 2011-04-22 00:00:00 |     2    | NULL
4  | 2011-02-20 00:00:00 |     4    | NULL
5  | 2011-03-21 00:00:00 |     4    | NULL
6  | 2011-04-22 00:00:00 |     4    | NULL

Delta is the Difference between the timestamps What i want is a result for Delta Timestamp (in Days) for the the previous row and the current row grouped by the user_id.

this should be the result:

Id | sessionDate         | user_id | Delta in Days
------------------------------------------------------
1  | 2011-02-20 00:00:00 |     2   | NULL
2  | 2011-02-21 00:00:00 |     2   | 1
3  | 2011-02-22 00:00:00 |     2   | 1
4  | 2011-02-20 00:00:00 |     4   | NULL
5  | 2011-02-23 00:00:00 |     4   | 3
6  | 2011-02-25 00:00:00 |     4   | 2

I already have a solution for a specific user_id:

SELECT user_id, sessionDate, 
abs(DATEDIFF((SELECT MAX(sessionDate) FROM session WHERE sessionDate <  t.sessionDate and user_id = 1), sessionDate)) as Delta_in_days  
FROM session AS t
WHERE t.user_id = 1 order by sessionDate asc

But for more user_ids i didn´t find any solution

Hope somebody can help me.

1

3 Answers 3

2

Try this:

drop table a;
create table a( id integer not null primary key, d datetime, user_id integer );
insert into a values (1,now() + interval 0 day, 1 );
insert into a values (2,now() + interval 1 day, 1 );
insert into a values (3,now() + interval 2 day, 1 );
insert into a values (4,now() + interval 0 day, 2 );
insert into a values (5,now() + interval 1 day, 2 );
insert into a values (6,now() + interval 2 day, 2 );

select t1.user_id, t1.d, t2.d, datediff(t2.d,t1.d)
from a t1, a t2
where t1.user_id=t2.user_id
and t2.d = (select min(d) from a t3 where t1.user_id=t3.user_id and t3.d > t1.d)

Which means: join your table to itself on user_ids and adjacent datetime entries and compute the difference.

1
  • 1
    Note that this solution does not scale well.
    – Strawberry
    Jun 2, 2014 at 10:09
1

If id is really sequential (as in your sample data), the following should be quite efficient:

select t.id, t.sessionDate, t.user_id, datediff(t2.sessiondate, t.sessiondate)
from table t left outer join
     table tprev
     on t.user_id = tprev.user_id and
        t.id = tprev.id + 1;

There is also another efficient method using variables. Something like this should work:

select t.id, t.sessionDate, t.user_id, datediff(prevsessiondate, sessiondate)
from (select t.*,
             if(@user_id = user_id, @prev, NULL) as prevsessiondate,
             @prev := sessiondate,
             @user_id := user_id
      from table t cross join
           (select @user_id := 0, @prev := 0) vars
      order by user_id, id
     ) t;

(There is a small issue with these queries where the variables in the select clause may not be evaluated in the order we expect them to. This is possible to fix, but it complicates the query and this will usually work.)

1
  • "using variables -- what do you have in mind? Please give us a hint. Jun 2, 2014 at 14:39
0

Although you have choosen an answer here is another way of achieving it

SELECT
t1.Id,
t1.sessionDate,
t1.user_id,
TIMESTAMPDIFF(DAY,t2.sessionDate,t1.sessionDate) as delta
from myTable t1
left join myTable t2
on t1.user_id = t2.user_id
AND t2.Id = (
  select max(Id) from myTable t3
  where t1.Id > t3.Id AND t1.user_id = t3.user_id

);

DEMO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.