mysql - pairing dates under same column in my sql -
how pair dates under same column in sql?
table looks this:
id | user | type | date ---+------------+-------------+------------- 1 | 1 | login | 2014-03-19 15:41:52 2 | 1 | login | 2014-03-19 19:41:52 3 | 1 | logout | 2014-03-19 21:41:52 4 | 2 | login | 2014-03-20 19:41:52 5 | 2 | logout | 2014-03-20 20:41:52 6 | 3 | login | 2014-03-21 19:41:52 7 | 4 | login | 2014-03-21 19:42:52 8 | 4 | logout | 2014-03-21 21:41:52 10 | 5 | login | 2014-03-19 21:45:52 11 | 5 | login | 2014-03-19 21:46:52 12 | 3 | logout | 2014-03-19 21:51:52 13 | 5 | logout | 2014-03-19 22:41:52
the aim pair 1 login 1 logout.
in example user 1 has 2 logins , 1 logout.
the first login should disregarded, , recent login should paired minimum logout: pairing login id=2 , logout id=3
something may work , should return combinations table, performance may getting worse table fills. better performance, consider adding unique generated sessionid table, same both login , logout.
select t1.user, t1.date logindate, t2.date logoutdate table t1 left join table t2 on t1.user = t2.user , t1.type = 'login' , t2.type = 'logout' , t1.date < t2.date left join table t3 on t3.user = t1.user , t3.type = 'login' , t3.date < t2.date t3.id null
Comments
Post a Comment