diff --git a/etl-scripts/calculated_tables/cohort_dates.sql b/etl-scripts/calculated_tables/cohort_dates.sql index 5a69bb5..1ab7b6f 100644 --- a/etl-scripts/calculated_tables/cohort_dates.sql +++ b/etl-scripts/calculated_tables/cohort_dates.sql @@ -280,7 +280,31 @@ insert into dates (endDate) values ("2022-09-30"), ("2022-10-31"), ("2022-11-30"), -("2022-12-31") +("2022-12-31"), +("2023-01-31"), +("2023-02-28"), +("2023-03-31"), +("2023-04-30"), +("2023-05-31"), +("2023-06-30"), +("2023-07-31"), +("2023-08-31"), +("2023-09-30"), +("2023-10-31"), +("2023-11-30"), +("2023-12-31"), +("2024-01-31"), +("2024-02-29"), +("2024-03-31"), +("2024-04-30"), +("2024-05-31"), +("2024-06-30"), +("2024-07-31"), +("2024-08-31"), +("2024-09-30"), +("2024-10-31"), +("2024-11-30"), +("2024-12-31") ; diff --git a/etl-scripts/stored-procedures/generate_cohort_dates.sql b/etl-scripts/stored-procedures/generate_cohort_dates.sql new file mode 100644 index 0000000..ecec44b --- /dev/null +++ b/etl-scripts/stored-procedures/generate_cohort_dates.sql @@ -0,0 +1,44 @@ +CREATE DEFINER=`henrykorir`@`%` PROCEDURE `generate_cohort_dates`() +BEGIN +DECLARE month_index INT DEFAULT 1; +DECLARE month_start_date VARCHAR(100) DEFAULT ""; +DECLARE max_end_date DATETIME DEFAULT NULL; +DECLARE end_date DATETIME DEFAULT NULL; +DECLARE MAX_MONTHS INT DEFAULT 12; +DECLARE end_day DATETIME DEFAULT CONCAT((YEAR(NOW()) + 1),"-12-31"); +DECLARE start_year INT DEFAULT 2000; + +CREATE TABLE IF NOT EXISTS dates ( + endDate DATETIME, + INDEX endDate (endDate) +); + +SELECT MAX(endDate) INTO max_end_date FROM dates; +-- SELECT max_end_date; + +IF (max_end_date IS NULL) THEN + SET start_year := 2000; +ELSE + SET start_year := YEAR(max_end_date) + 1; +END IF; + +-- SELECT start_year; +LOOP_PER_YEAR: LOOP + WHILE (month_index <= MAX_MONTHS) DO + + SET month_start_date := CONCAT(start_year,'-',month_index,'-01'); + + SELECT LAST_DAY(month_start_date) INTO end_date; + + INSERT INTO dates (endDate) VALUES(end_date); + + SET month_index := month_index + 1; + + END WHILE; + SET month_index := 1; + SET start_year := start_year + 1; + IF (start_year > YEAR(end_day)) THEN + LEAVE LOOP_PER_YEAR; -- exit years loop + END IF; + END LOOP; +END \ No newline at end of file