WITH date1 AS (
	SELECT generate_series('2018-01-01'::timestamp, '2021-12-31'::timestamp, '1 day') AS ts
), date2 AS (
	SELECT get_date_primary_key(ts) AS DATE_ID,
		EXTRACT(YEAR FROM ts) AS N_YEAR,
		EXTRACT(MONTH FROM ts) AS N_MONTH,
		EXTRACT(WEEK FROM ts) AS N_WEEK,
		EXTRACT(DAY FROM ts) AS DAY_MONTH,
        EXTRACT(ISODOW FROM ts) AS DAY_WEEK
	FROM date1
), date3 AS (
	SELECT
		*,
		CASE
			WHEN N_MONTH = 1 THEN 'January'
			WHEN N_MONTH = 2 THEN 'February'
			WHEN N_MONTH = 3 THEN 'March'
			WHEN N_MONTH = 4 THEN 'April'
			WHEN N_MONTH = 5 THEN 'May'
			WHEN N_MONTH = 6 THEN 'June'
			WHEN N_MONTH = 7 THEN 'July'
			WHEN N_MONTH = 8 THEN 'August'
	 		WHEN N_MONTH = 9 THEN 'September'
			WHEN N_MONTH = 10 THEN 'October'
			WHEN N_MONTH = 11 THEN 'November'
			WHEN N_MONTH = 12 THEN 'December'
		END AS MONTH_NAME,
		CASE
			WHEN DAY_WEEK = 1 THEN 'Monday'
			WHEN DAY_WEEK = 2 THEN 'Tuesday'
			WHEN DAY_WEEK = 3 THEN 'Wednesday'
			WHEN DAY_WEEK = 4 THEN 'Thursday'
			WHEN DAY_WEEK = 5 THEN 'Friday'
			WHEN DAY_WEEK = 6 THEN 'Saturday'
			WHEN DAY_WEEK = 7 THEN 'Sunday'
		END AS DAY_NAME
	FROM date2
)

INSERT INTO public."DIM_DATE"
	SELECT DATE_ID,N_YEAR,N_MONTH,MONTH_NAME,N_WEEK,DAY_MONTH,DAY_NAME
	FROM date3;