MySQL Build date dimension table

Source: Internet
Author: User

The first step is to create the function f_dim_day


CREATE PROCEDURE F_dim_day (in Start_date VARCHAR (a), in Date_count int)
Begin
declare i int;
Set i=0;
DELETE from Dim_day;
While I<date_count do

INSERT into Dim_day
(Day_id,day_short_desc,day_long_desc,week_id,week_long_desc,month_id,month_short_desc,month_long_desc,quarter_ ID,QUARTER_LONG_DESC,YEAR_ID,YEAR_LONG_DESC)
SELECT
REPLACE (start_date, '-', ') day_id,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%y-%m-%d ') Day_short_desc,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y year%m month%d day ') Day_long_desc,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%y%u ') week_id,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y year%u week ') Week_long_desc,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%y%m ') month_id,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%y-%m ') Month_short_desc,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y-year%m month ') Month_long_desc,
CONCAT (Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y '), Quarter (Str_to_date (start_date, '%y-%m-%d%H :%i:%s ')) quarter_id,
CONCAT (Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y '), ' The Year ', Quarter (str_to_date (start_date, '%y-% m-%d%h:%i:%s '), ' quarter ') Quarter_long_desc,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y ') year_id,
Date_format (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), '%Y year ') Year_long_desc
from dual;
Set i=i+1;
Set start_date = Date_format (Date_add (Str_to_date (start_date, '%y-%m-%d%h:%i:%s '), Interval 1 day), '%y-%m-%d ');
End while;

End

In the second step, the calling function passes the start time and the number of days, executing the following sql:

Call F_dim_day (' 2015-01-01 ', 365)


The resulting effect is as follows:



MySQL Build date dimension table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.