Last active
October 4, 2023 19:24
-
-
Save sunnycmf/2e8c2b5aa72d77a0dd96 to your computer and use it in GitHub Desktop.
MySQL Time Dimension generation SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- credit to Akom's Tech Ruminations | |
-- http://tech.akom.net/archives/36-Creating-A-Basic-Date-Dimension-Table-in-MySQL.html | |
CREATE TABLE IF NOT EXISTS time_d ( | |
time_id INT NOT NULL auto_increment, | |
fulltime time, | |
hour int, | |
minute int, | |
second int, | |
ampm varchar(2), | |
PRIMARY KEY(time_id) | |
) ENGINE=InnoDB AUTO_INCREMENT=1000; | |
delimiter // | |
DROP PROCEDURE IF EXISTS timedimbuild; | |
CREATE PROCEDURE timedimbuild () | |
BEGIN | |
DECLARE v_full_date DATETIME; | |
DELETE FROM time_d; | |
SET v_full_date = '2009-03-01 00:00:00'; | |
WHILE v_full_date < '2009-03-02 00:00:00' DO | |
INSERT INTO time_d ( | |
fulltime , | |
hour , | |
minute , | |
second , | |
ampm | |
) VALUES ( | |
TIME(v_full_date), | |
HOUR(v_full_date), | |
MINUTE(v_full_date), | |
SECOND(v_full_date), | |
DATE_FORMAT(v_full_date,'%p') | |
); | |
SET v_full_date = DATE_ADD(v_full_date, INTERVAL 1 SECOND); | |
END WHILE; | |
END; | |
// | |
delimiter ; | |
-- call the stored procedure | |
call timedimbuild(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for Date dimension generation, refer:
https://gist.github.com/sunnycmf/131a10a17d226e2ffb69