salix/db/changes/10110-postCampaign/00-rangeDateInfo.sql

42 lines
1.0 KiB
SQL

DROP procedure IF EXISTS `vn`.`rangeDateInfo`;
DELIMITER $$
CREATE PROCEDURE `vn`.`rangeDateInfo` (vStarted DATE, vEnded DATE)
BEGIN
/**
* Crea una tabla temporal con las fechas
* desde una fecha inicial a una final
* @param vStarted fecha inicial
* @param vEnded fecha final
*/
DECLARE vDated DATE DEFAULT vStarted;
DROP TEMPORARY TABLE IF EXISTS tmp.rangeDate;
CREATE TEMPORARY TABLE tmp.rangeDate(
`dated` DATE,
`period` INT,
`month` INT,
`year` INT,
`day` INT,
`week` INT,
`yearMonth` INT
)
ENGINE = MEMORY;
WHILE vDated <= vEnded DO
INSERT INTO tmp.rangeDate
SET `dated` = vDated,
`period` = YEAR(vDated) * 100 + WEEK(vDated, 1),
`month` = MONTH(vDated),
`year` = YEAR(vDated),
`day` = DAY(vDated),
`week` = WEEK(vDated, 1),
`yearMonth` = YEAR(vDated) * 100 + MONTH(vDated);
SET vDated = DATE_ADD(vDated, INTERVAL 1 DAY);
END WHILE;
END$$
DELIMITER ;