1、建一张表test_date用来日期的自增长,里面只放一列 ID,写一个存储,插入顺序数字
BEGIN #Routine body goes here... declare id int; set id=1; while id<=6000 do insert into test_date values(id); set id=id+1; end while; END
2. 建立时间维度表,插入记录
SET @d0 = "2000-01-01"; SET @d1 = "2020-12-31"; SET @str = "Q"; SET @date = date_sub(@d0, interval 1 day); INSERT INTO dim_time SELECT @date := date_add(@date, interval 1 day), year(@date), month(@date), DAYOFYEAR(@date), day(@date) as d, weekday(@date)+1 as wd, week(@date, 3) as w, dayname(@date) as wd_name, monthname(@date) as m_name, quarter(@date) as q, CONCAT(@str,quarter(@date)) as qq, DAYOFMONTH(last_day(@date)), date_format(@date, "%Y%m%d"), date_format(@date, ‘%d-%m-%Y‘)as yw FROM test_date WHERE date_add(@date, interval 1 day) <= @d1 ORDER BY id;
时间: 2024-10-05 07:34:06