DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerDepartmentByDate`(vDate DATE) BEGIN DROP TEMPORARY TABLE IF EXISTS tmp.workerDepartmentByDate; CREATE TEMPORARY TABLE tmp.workerDepartmentByDate ENGINE = MEMORY SELECT w.id userFk, w.lastName name, w.firstName firstname, d.name department, d.id departmentFk, b.started, d.isProduction, CAST(12 * b.amount / ct.hoursWeek / 52 AS DECIMAL(10,2)) costeHora, w.fi nif FROM business b JOIN calendarType ct ON ct.id = b.calendarTypeFk JOIN department d ON d.id = b.departmentFk JOIN worker w ON w.id = b.workerFk WHERE b.started <= vDate AND (b.ended > vDate OR b.ended IS NULL); END$$ DELIMITER ;