DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerTimeControl_getClockIn`( vUserFk INT, vDated DATE) BEGIN /** * Muestra una tabla con los 7 días inmediatamente anteriores a la fecha proporcionada * y las fichadas de cada día, en columnas por día * @param vUserFk Identificador del trabajador * @param vDated Fecha proporcionada */ CALL timeControl_calculateByWorker( vUserFk, DATE_SUB(vDated, INTERVAL 6 DAY), util.dayEnd(vDated)); SET @position := 0; SET @lastDated := NULL; SELECT * FROM( SELECT MAX(6daysAgo) 6daysAgo, MAX(6daysAgoDirection) 6daysAgoDirection, MAX(5daysAgo) 5daysAgo, MAX(5daysAgoDirection) 5daysAgoDirection, MAX(4daysAgo) 4daysAgo, MAX(4daysAgoDirection) 4daysAgoDirection, MAX(3daysAgo) 3daysAgo, MAX(3daysAgoDirection) 3daysAgoDirection, MAX(2daysAgo) 2daysAgo, MAX(2daysAgoDirection) 2daysAgoDirection, MAX(1daysAgo) 1daysAgo, MAX(1daysAgoDirection) 1daysAgoDirection, MAX(0daysAgo) 0daysAgo, MAX(0daysAgoDirection) 0daysAgoDirection FROM(SELECT IF(daysAgo = 6, timed, '') 6daysAgo, IF(daysAgo = 5, timed, '') 5daysAgo, IF(daysAgo = 4, timed, '') 4daysAgo, IF(daysAgo = 3, timed, '') 3daysAgo, IF(daysAgo = 2, timed, '') 2daysAgo, IF(daysAgo = 1, timed, '') 1daysAgo, IF(daysAgo = 0, timed, '') 0daysAgo, IF(daysAgo = 6, direction, '') 6daysAgoDirection, IF(daysAgo = 5, direction, '') 5daysAgoDirection, IF(daysAgo = 4, direction, '') 4daysAgoDirection, IF(daysAgo = 3, direction, '') 3daysAgoDirection, IF(daysAgo = 2, direction, '') 2daysAgoDirection, IF(daysAgo = 1, direction, '') 1daysAgoDirection, IF(daysAgo = 0, direction, '') 0daysAgoDirection, direction, position FROM(SELECT DATEDIFF(vDated, timed) daysAgo, TIME_FORMAT(timed,'%H:%i') timed, IF(DATE(timed) <> @lastDated, @position := 0, NULL), @position := @position+1 position, @lastDated := DATE(timed), direction FROM workerTimeControl wtc WHERE wtc.userFk = vUserFk AND wtc.timed >= TIMESTAMPADD(DAY, -6,vDated) ORDER BY wtc.timed LIMIT 10000000000000000000 )sub )sub2 GROUP BY position) sub3 JOIN (SELECT MAX(IF(daysAgo = 6, timeWorkSeconds, 0)) 6daysAgoTotal, MAX(IF(daysAgo = 5, timeWorkSeconds, 0)) 5daysAgoTotal, MAX(IF(daysAgo = 4, timeWorkSeconds, 0)) 4daysAgoTotal, MAX(IF(daysAgo = 3, timeWorkSeconds, 0)) 3daysAgoTotal, MAX(IF(daysAgo = 2, timeWorkSeconds, 0)) 2daysAgoTotal, MAX(IF(daysAgo = 1, timeWorkSeconds, 0)) 1daysAgoTotal, MAX(IF(daysAgo = 0, timeWorkSeconds, 0)) 0daysAgoTotal FROM (SELECT DATEDIFF(vDated, dated) daysAgo, timeWorkSeconds FROM tmp.timeControlCalculate) sub4)sub5 ON TRUE; DROP TEMPORARY TABLE tmp.timeControlCalculate; END$$ DELIMITER ;