You could get it using MIN
,MAX
and WEEKDAY
with your actual design:
Table:
CREATE TABLE `test`.`timeclock` (
`id` INT NOT NULL AUTO_INCREMENT,
`user` INT NULL,
`timeinout` DATETIME NULL,
`type` ENUM('in', 'out') NULL,
`function` INT NULL,
PRIMARY KEY (`id`) );
Data:
insert into timeclock(id,user,timeinout,type,function)VALUES
('1','1','2015-07-27 20:20:32','in','1'),
('2','1','2015-07-27 20:20:56','out','1'),
('3','1','2015-07-27 20:29:25','in','1'),
('4','1','2015-07-28 20:32:04','out','2'),
('5','1','2015-07-28 20:36:28','in','1'),
('6','1','2015-07-29 20:40:00','out','1'),
('7','1','2015-07-29 20:45:39','in','1'),
('8','1','2015-07-30 20:20:32','in','1'),
('9','1','2015-07-30 20:20:56','out','1'),
('10','1','2015-07-31 20:29:25','in','1'),
('11','1','2015-07-31 20:32:04','out','2'),
('12','1','2015-08-01 20:36:28','in','1'),
('13','1','2015-08-01 20:40:00','out','1'),
('14','1','2015-08-02 20:20:32','in','1'),
('15','1','2015-08-02 20:20:56','out','1'),
('16','1','2015-08-02 20:29:25','in','1'),
('17','1','2015-08-02 20:32:04','out','2'),
('18','1','2015-08-02 20:36:28','in','1'),
('19','1','2015-08-02 20:40:00','out','1');
Query:
SELECT
tc.user,
tcmin.min,
tcmax.max,
CASE
WHEN WEEKDAY(tc.timeinout)=0 THEN 'Monday'
WHEN WEEKDAY(tc.timeinout)=1 THEN 'Tuesday'
WHEN WEEKDAY(tc.timeinout)=2 THEN 'Wednesday'
WHEN WEEKDAY(tc.timeinout)=3 THEN 'Thursday'
WHEN WEEKDAY(tc.timeinout)=4 THEN 'Friday'
WHEN WEEKDAY(tc.timeinout)=5 THEN 'Saturday'
WHEN WEEKDAY(tc.timeinout)=6 THEN 'Sunday' END AS WeekDay,
TIMESTAMPDIFF(MINUTE,tcmin.min,tcmax.max) AS Minutes
FROM test.timeclock AS tc
LEFT JOIN (SELECT
user,
MIN(timeinout) as min
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmin
ON (tcmin.user=tc.user AND DATE(tcmin.min)=DATE(tc.timeinout))
LEFT JOIN (SELECT
user,
MAX(timeinout) as max
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmax
ON (tcmax.user=tc.user AND DATE(tcmax.max)=DATE(tc.timeinout))
WHERE
tc.timeinout>='2015-07-27'
AND tc.timeinout<'2015-08-03'
AND tc.user=1
GROUP BY tc.user,DATE(tc.timeinout)
ORDER BY tc.timeinout ASC;
Test:
mysql> SELECT
-> tc.user,
-> tcmin.min,
-> tcmax.max,
-> CASE
-> WHEN WEEKDAY(tc.timeinout)=0 THEN 'Monday'
-> WHEN WEEKDAY(tc.timeinout)=1 THEN 'Tuesday'
-> WHEN WEEKDAY(tc.timeinout)=2 THEN 'Wednesday'
-> WHEN WEEKDAY(tc.timeinout)=3 THEN 'Thursday'
-> WHEN WEEKDAY(tc.timeinout)=4 THEN 'Friday'
-> WHEN WEEKDAY(tc.timeinout)=5 THEN 'Saturday'
-> WHEN WEEKDAY(tc.timeinout)=6 THEN 'Sunday' END AS WeekDay,
-> TIMESTAMPDIFF(MINUTE,tcmin.min,tcmax.max) AS Minutes
-> FROM test.timeclock AS tc
-> LEFT JOIN (SELECT
-> user,
-> MIN(timeinout) as min
-> FROM test.timeclock
-> GROUP BY user,DATE(timeinout)) as tcmin
-> ON (tcmin.user=tc.user AND DATE(tcmin.min)=DATE(tc.timeinout))
-> LEFT JOIN (SELECT
-> user,
-> MAX(timeinout) as max
-> FROM test.timeclock
-> GROUP BY user,DATE(timeinout)) as tcmax
-> ON (tcmax.user=tc.user AND DATE(tcmax.max)=DATE(tc.timeinout))
-> WHERE
-> tc.timeinout>='2015-07-27'
-> AND tc.timeinout<'2015-08-03'
-> AND tc.user=1
-> GROUP BY tc.user,DATE(tc.timeinout)
-> ORDER BY tc.timeinout ASC;
+------+---------------------+---------------------+-----------+---------+
| user | min | max | WeekDay | Minutes |
+------+---------------------+---------------------+-----------+---------+
| 1 | 2015-07-27 20:20:32 | 2015-07-27 20:29:25 | Monday | 8 |
| 1 | 2015-07-28 20:32:04 | 2015-07-28 20:36:28 | Tuesday | 4 |
| 1 | 2015-07-29 20:40:00 | 2015-07-29 20:45:39 | Wednesday | 5 |
| 1 | 2015-07-30 20:20:32 | 2015-07-30 20:20:56 | Thursday | 0 |
| 1 | 2015-07-31 20:29:25 | 2015-07-31 20:32:04 | Friday | 2 |
| 1 | 2015-08-01 20:36:28 | 2015-08-01 20:40:00 | Saturday | 3 |
| 1 | 2015-08-02 20:20:32 | 2015-08-02 20:40:00 | Sunday | 19 |
+------+---------------------+---------------------+-----------+---------+
7 rows in set (0.00 sec)
mysql>
EDIT 2:
SELECT
user,
'2020-01-01' as date,
'Total for current results:' as WeekDay,
SUM(Minutes) as Minutes
FROM (SELECT
tc.user,
DATE(tc.timeinout) as date,
CASE
WHEN WEEKDAY(tc.timeinout)=0 THEN 'Monday'
WHEN WEEKDAY(tc.timeinout)=1 THEN 'Tuesday'
WHEN WEEKDAY(tc.timeinout)=2 THEN 'Wednesday'
WHEN WEEKDAY(tc.timeinout)=3 THEN 'Thursday'
WHEN WEEKDAY(tc.timeinout)=4 THEN 'Friday'
WHEN WEEKDAY(tc.timeinout)=5 THEN 'Saturday'
WHEN WEEKDAY(tc.timeinout)=6 THEN 'Sunday' END AS WeekDay,
TIMESTAMPDIFF(MINUTE,tcmin.min,tcmax.max) AS Minutes
FROM test.timeclock AS tc
LEFT JOIN (SELECT
user,
MIN(timeinout) as min
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmin
ON (tcmin.user=tc.user AND DATE(tcmin.min)=DATE(tc.timeinout))
LEFT JOIN (SELECT
user,
MAX(timeinout) as max
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmax
ON (tcmax.user=tc.user AND DATE(tcmax.max)=DATE(tc.timeinout))
WHERE
tc.timeinout>='2015-07-27'
AND tc.timeinout<'2015-08-03'
AND tc.user=1
GROUP BY tc.user,DATE(tc.timeinout)
ORDER BY date ASC) AS Total
UNION ALL
SELECT
tc.user,
DATE(tc.timeinout) as date,
CASE
WHEN WEEKDAY(tc.timeinout)=0 THEN 'Monday'
WHEN WEEKDAY(tc.timeinout)=1 THEN 'Tuesday'
WHEN WEEKDAY(tc.timeinout)=2 THEN 'Wednesday'
WHEN WEEKDAY(tc.timeinout)=3 THEN 'Thursday'
WHEN WEEKDAY(tc.timeinout)=4 THEN 'Friday'
WHEN WEEKDAY(tc.timeinout)=5 THEN 'Saturday'
WHEN WEEKDAY(tc.timeinout)=6 THEN 'Sunday' END AS WeekDay,
TIMESTAMPDIFF(MINUTE,tcmin.min,tcmax.max) AS Minutes
FROM test.timeclock AS tc
LEFT JOIN (SELECT
user,
MIN(timeinout) as min
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmin
ON (tcmin.user=tc.user AND DATE(tcmin.min)=DATE(tc.timeinout))
LEFT JOIN (SELECT
user,
MAX(timeinout) as max
FROM test.timeclock
GROUP BY user,DATE(timeinout)) as tcmax
ON (tcmax.user=tc.user AND DATE(tcmax.max)=DATE(tc.timeinout))
WHERE
tc.timeinout>='2015-07-27'
AND tc.timeinout<'2015-08-03'
AND tc.user=1
GROUP BY tc.user,DATE(tc.timeinout)
ORDER BY date ASC;
in
andout
from each day?2015-08-05 20:20:32
AND2015-08-05 20:40:00
? Or every time spend between2015-08-05 20:20:32
asin
and2015-08-05 20:20:56
asout
?