-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidterm1sql.sql
More file actions
137 lines (122 loc) · 5.08 KB
/
midterm1sql.sql
File metadata and controls
137 lines (122 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
USE destdb;
DROP TABLE IF EXISTS dim_date;
CREATE TABLE dim_date(
date_key int NOT NULL auto_increment,
full_date date NULL,
date_name char(11) NOT NULL,
date_name_us char(11) NOT NULL,
date_name_eu char(11) NOT NULL,
day_of_week tinyint NOT NULL,
day_name_of_week char(10) NOT NULL,
day_of_month tinyint NOT NULL,
day_of_year smallint NOT NULL,
weekday_weekend char(10) NOT NULL,
week_of_year tinyint NOT NULL,
month_name char(10) NOT NULL,
month_of_year tinyint NOT NULL,
is_last_day_of_month char(1) NOT NULL,
calendar_quarter tinyint NOT NULL,
calendar_year smallint NOT NULL,
calendar_year_month char(10) NOT NULL,
calendar_year_qtr char(10) NOT NULL,
fiscal_month_of_year tinyint NOT NULL,
fiscal_quarter tinyint NOT NULL,
fiscal_year int NOT NULL,
fiscal_year_month char(10) NOT NULL,
fiscal_year_qtr char(10) NOT NULL,
PRIMARY KEY (`date_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Here is the PopulateDateDimension Stored Procedure:
delimiter //
DROP PROCEDURE IF EXISTS PopulateDateDimension//
CREATE PROCEDURE PopulateDateDimension(BeginDate DATETIME, EndDate DATETIME)
BEGIN
# =============================================
# Description: http://arcanecode.com/2009/11/18/populating-a-kimball-date-dimension/
# =============================================
# A few notes, this code does nothing to the existing table, no deletes are triggered before hand.
# Because the DateKey is uniquely indexed, it will simply produce errors if you attempt to insert duplicates.
# You can however adjust the Begin/End dates and rerun to safely add new dates to the table every year.
# If the begin date is after the end date, no errors occur but nothing happens as the while loop never executes.
# Holds a flag so we can determine if the date is the last day of month
DECLARE LastDayOfMon CHAR(1);
# Number of months to add to the date to get the current Fiscal date
DECLARE FiscalYearMonthsOffset INT;
# These two counters are used in our loop.
DECLARE DateCounter DATETIME; #Current date in loop
DECLARE FiscalCounter DATETIME; #Fiscal Year Date in loop
# Set this to the number of months to add to the current date to get the beginning of the Fiscal year.
# For example, if the Fiscal year begins July 1, put a 6 there.
# Negative values are also allowed, thus if your 2010 Fiscal year begins in July of 2009, put a -6.
SET FiscalYearMonthsOffset = 6;
# Start the counter at the begin date
SET DateCounter = BeginDate;
WHILE DateCounter <= EndDate DO
# Calculate the current Fiscal date as an offset of the current date in the loop
SET FiscalCounter = DATE_ADD(DateCounter, INTERVAL FiscalYearMonthsOffset MONTH);
# Set value for IsLastDayOfMonth
IF MONTH(DateCounter) = MONTH(DATE_ADD(DateCounter, INTERVAL 1 DAY)) THEN
SET LastDayOfMon = 'N';
ELSE
SET LastDayOfMon = 'Y';
END IF;
# add a record into the date dimension table for this date
INSERT INTO dim_date
(date_key
, full_date
, date_name
, date_name_us
, date_name_eu
, day_of_week
, day_name_of_week
, day_of_month
, day_of_year
, weekday_weekend
, week_of_year
, month_name
, month_of_year
, is_last_day_of_month
, calendar_quarter
, calendar_year
, calendar_year_month
, calendar_year_qtr
, fiscal_month_of_year
, fiscal_quarter
, fiscal_year
, fiscal_year_month
, fiscal_year_qtr)
VALUES (
( YEAR(DateCounter) * 10000 ) + ( MONTH(DateCounter) * 100 ) + DAY(DateCounter) #DateKey
, DateCounter #FullDate
, CONCAT(CAST(YEAR(DateCounter) AS CHAR(4)),'/', DATE_FORMAT(DateCounter,'%m'),'/', DATE_FORMAT(DateCounter,'%d')) #DateName
, CONCAT(DATE_FORMAT(DateCounter,'%m'),'/', DATE_FORMAT(DateCounter,'%d'),'/', CAST(YEAR(DateCounter) AS CHAR(4)))#DateNameUS
, CONCAT(DATE_FORMAT(DateCounter,'%d'),'/', DATE_FORMAT(DateCounter,'%m'),'/', CAST(YEAR(DateCounter) AS CHAR(4)))#DateNameEU
, DAYOFWEEK(DateCounter) #DayOfWeek
, DAYNAME(DateCounter) #DayNameOfWeek
, DAYOFMONTH(DateCounter) #DayOfMonth
, DAYOFYEAR(DateCounter) #DayOfYear
, CASE DAYNAME(DateCounter)
WHEN 'Saturday' THEN 'Weekend'
WHEN 'Sunday' THEN 'Weekend'
ELSE 'Weekday'
END #WeekdayWeekend
, WEEKOFYEAR(DateCounter) #WeekOfYear
, MONTHNAME(DateCounter) #MonthName
, MONTH(DateCounter) #MonthOfYear
, LastDayOfMon #IsLastDayOfMonth
, QUARTER(DateCounter) #CalendarQuarter
, YEAR(DateCounter) #CalendarYear
, CONCAT(CAST(YEAR(DateCounter) AS CHAR(4)),'-',DATE_FORMAT(DateCounter,'%m')) #CalendarYearMonth
, CONCAT(CAST(YEAR(DateCounter) AS CHAR(4)),'Q',QUARTER(DateCounter)) #CalendarYearQtr
, MONTH(FiscalCounter) #[FiscalMonthOfYear]
, QUARTER(FiscalCounter) #[FiscalQuarter]
, YEAR(FiscalCounter) #[FiscalYear]
, CONCAT(CAST(YEAR(FiscalCounter) AS CHAR(4)),'-',DATE_FORMAT(FiscalCounter,'%m')) #[FiscalYearMonth]
, CONCAT(CAST(YEAR(FiscalCounter) AS CHAR(4)),'Q',QUARTER(FiscalCounter)) #[FiscalYearQtr]
);
# Increment the date counter for next pass thru the loop
SET DateCounter = DATE_ADD(DateCounter, INTERVAL 1 DAY);
END WHILE;
END//
CALL PopulateDateDimension('2000-01-01', '2022-12-31');
SELECT * FROM dim_date;