From d172bffbedce09e9cbb7442ac0924badf9350e72 Mon Sep 17 00:00:00 2001 From: henrykorir Date: Sat, 30 Dec 2023 04:51:37 +0300 Subject: [PATCH 1/2] Added cohort dates for 2023 and 2024 --- .../calculated_tables/cohort_dates.sql | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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") ; From 1f7155e5d9a1078ee7c36e4d6494b34faa19347d Mon Sep 17 00:00:00 2001 From: henrykorir Date: Sat, 30 Dec 2023 20:30:28 +0300 Subject: [PATCH 2/2] [Proof-of-concept]: added a stored procedure to generate cohort dates for reports --- .../generate_cohort_dates.sql | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 etl-scripts/stored-procedures/generate_cohort_dates.sql 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