salix/db/routines/vn/procedures/workerTimeControl_calculate...

35 lines
1022 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerTimeControl_calculateOddDays`()
BEGIN
/**
* Calculo de las fichadas impares por empleado y dia.
*
* @return tmp.workerTimeControlOddDays (userFk, dated)
*/
SET @cont := FALSE;
SET @lastUserFk:= NULL;
DROP TEMPORARY TABLE IF EXISTS tmp.workerTimeControlOddDays;
CREATE TEMPORARY TABLE tmp.workerTimeControlOddDays
SELECT sub2.userFk workerFk, sub2.dated
FROM (
SELECT userFk,
dated,
COUNT(*) cont2
FROM (
SELECT DATE(wtc.timed) dated,
userFk,
IFNULL(@cont, @cont := TRUE ),
IF(direction = 'in' OR userFk <> @lastUserFk, @cont := @cont+1, TRUE),
@cont cont,
@lastUserFk := userFk
FROM workerTimeControl wtc
WHERE wtc.timed BETWEEN TIMESTAMPADD(YEAR,-1,util.VN_CURDATE()) AND util.dayEnd(util.VN_CURDATE())
ORDER BY userFk, timed
LIMIT 10000000000000000000)sub
GROUP BY cont
HAVING cont2 MOD 2 = 1
)sub2;
END$$
DELIMITER ;