Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cohort dates for 2023 and 2024 #284

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion etl-scripts/calculated_tables/cohort_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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")
;


Expand Down
44 changes: 44 additions & 0 deletions etl-scripts/stored-procedures/generate_cohort_dates.sql
Original file line number Diff line number Diff line change
@@ -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