Search code examples
phpmysqltime-and-attendance

Calculate difference between rows for each day in month based on type


I want to export data for specific month (based on user input) from MySQL table, I wan't to calculate difference between created_at for each day in that month.

attendance_id:

  • 1 = arrival
  • 2 = exit
  • 10 = work from home

When user gets to work his entrance is logged as 1 (arrival), when he leaves work his exit is logged as 2 (arrival). Users can also work from home, when they start to work its logged as 10 (work from home) when they stop its logged as 2 (exit). Table user_attendance

+-----+---------+---------------+----------------+----------------+
| id  | user_id | attendance_id |   created_at   |   updated_at   |
+-----+---------+---------------+----------------+----------------+
|  52 |      45 |             1 | 1.4.2015 6:48  | 1.4.2015 6:48  |
|  53 |      31 |             1 | 1.4.2015 6:52  | 1.4.2015 6:52  |
| 132 |      45 |             2 | 1.4.2015 13:58 | 1.4.2015 13:58 |
| 133 |      31 |             2 | 1.4.2015 14:32 | 1.4.2015 14:32 |
| 159 |      45 |             1 | 2.4.2015 6:49  | 2.4.2015 6:49  |
| 160 |      31 |             1 | 2.4.2015 6:52  | 2.4.2015 6:52  |
| 232 |      31 |             2 | 2.4.2015 15:06 | 2.4.2015 15:06 |
| 233 |      45 |             2 | 2.4.2015 15:09 | 2.4.2015 15:09 |
| 252 |      74 |            10 | 3.4.2015 6:52  | 3.4.2015 6:52  |
| 253 |      74 |             2 | 3.4.2015 15:52 | 3.4.2015 15:52 |
+-----+---------+---------------+----------------+----------------+

SQL:

CREATE TABLE IF NOT EXISTS `user_attendance` (
`id` int(11) NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `attendance_id` int(10) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2836 ;

My attempt:

SELECT A.user_id, A.created_at,HOUR(TIMEDIFF(B.created_at,A.created_at)) AS timedifference
FROM user_attendance A CROSS JOIN user_attendance B
WHERE B.id IN (SELECT MIN(C.id) FROM user_attendance C WHERE C.id > A.id) and A.user_id=45 and A.attendance_id in(1,2) and MONTH(A.created_at)=5
ORDER BY date(A.created_at) ASC

As you can see I have limited it to specific user and to two types (arrival and exit) but the results are not OK, because timedifference is 0.


Solution

  •       select  ua.day(a.created_at) as day,ua.user_id, HOUR(timediff(max(created_at),min(created_at))) as TimeDiff
    from user_attendance ua
    where ua.created_at between '2015-05-01 00:00:00' and '2015-05-31 23:59:59'
    group by day(ua.created_at),ua.user_id
    order by ua.user_id