Thank you for this video, I've converted it into Stored Procedure to make it more dynamic. Please find it below : /* The stored procedure sp_create_calendar_dim_table is to generate a calendar dimension table (cal_dim_new) containing a range of dates between the specified start and end dates as parameters. */ -- Creation of Sp : sp_create_calender_dim_table CREATE PROCEDURE sp_create_calender_dim_table @start_date date, @end_date date as BEGIN with recursive_cte as ( Select @start_date as cal_date union all Select dateadd(dd,1,cal_date) as cal_date from recursive_cte where cal_date < @end_date ) Select row_number() over(order by (select null)) as id, cal_date, datepart(year,cal_date) as cal_year, datepart(dayofyear,cal_date) as cal_year_day, datepart(quarter,cal_date) as cal_quarter, datepart(month,cal_date) as cal_month, datename(month,cal_date) as cal_month_name, datepart(day,cal_date) as cal_month_day, datepart(week,cal_date) as cal_week, datepart(weekday,cal_date) as cal_week_day, datename(weekday,cal_date) as cal_day_name into cal_dim_new from recursive_cte option (maxrecursion 0) END; -- Execute Sp : sp_create_calender_dim_table EXEC sp_create_calender_dim_table @start_date = '2000-01-01', @end_date = '2050-12-31' -- Check the cal_dim_new table Select * from cal_dim_new
Hi Ankit, it is a great learning, I have been following you from the beginning, would you please implement day light saving logic in the where condition, ex: my views delivering the report every 1 hour to the business
Please make a video on subquery. I am not able to understand how to solve subqueries problem. I tried a lot from different different resources but not able to solve it . And if there is any cheat method for this then also please share. Thankyou
Sir is it correct according to you ? WITH cte AS ( SELECT CAST('2000-01-01' AS date) AS cal_date UNION ALL SELECT DATEADD(DAY, 1, cal_date) AS cal_date FROM cte WHERE cal_date < CAST('2050-12-30' AS date) ) SELECT cal_date, DATEPART(YEAR, cal_date) AS cal_year, DATEPART(dayofyear, cal_date) AS cal_year_day, DATEPART(quarter, cal_date) AS cal_quarter, DATEPART(month, cal_date) AS cal_month, DATENAME(month, cal_date) AS cal_month_name, DATEPART(day, cal_date) AS cal_month_day, DATEPART(week, cal_date) AS cal_week, DATEPART(weekday, cal_date) AS cal_week_day, DATENAME(weekday, cal_date) AS cal_day_name INTO calendar_table FROM cte OPTION (MAXRECURSION 32676);
Hey Ankit, Can you please help me with how to create this dimension table in mysql I am done with all the script following your video but I got stuck at last point when we need to create the physical table. This query is not working in mysql....please help. SELECT row_number() over(order by cal_date asc) as id , * into calendar_dimension from cte ; SET SESSION cte_max_recursion_depth = 1000000; Thanks in advance
@@ankitbansal6 Hello Ankit, Can you help in correcting the mistake, below is the script I'm using in MYSQL CREATE TABLE IF NOT EXISTS calendar_dim ( cal_date DATE, cal_year INT, cal_year_day INT, cal_quarter INT, cal_month INT, cal_month_name VARCHAR(20), cal_month_day INT, cal_week INT, cal_week_day INT, cal_day_name VARCHAR(20) ); -- Use the recursive CTE to generate data insert into calender_dim WITH RECURSIVE cte AS ( -- Anchor member SELECT CAST('2022-01-01' AS DATE) AS cal_date, YEAR(CAST('2022-01-01' AS DATE)) AS cal_year, DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day, QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter, MONTH(CAST('2022-01-01' AS DATE)) AS cal_month, MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name, DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day, WEEK(CAST('2022-01-01' AS DATE)) AS cal_week, DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day, DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name UNION ALL -- Recursive member SELECT ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date, YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year, DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day, QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter, MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month, MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name, DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day, WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week, DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day, DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name FROM cte WHERE cal_date < CAST('2025-01-10' AS DATE) ) select * from calender_dim; Getting zero record insert into calender_dim
@@ankitbansal6 sir , If I remove the insert into statement then I'm not getting any record from calender_dim table. It is not inserting the value into calender_dim table and since we have created CTE so I cannot use that CTE outside the recursive table . see the script below. CREATE TABLE IF NOT EXISTS calendar_dim ( cal_date DATE, cal_year INT, cal_year_day INT, cal_quarter INT, cal_month INT, cal_month_name VARCHAR(20), cal_month_day INT, cal_week INT, cal_week_day INT, cal_day_name VARCHAR(20) ); -- Use the recursive CTE to generate data insert into calender_dim WITH RECURSIVE cte AS ( -- Anchor member SELECT CAST('2022-01-01' AS DATE) AS cal_date, YEAR(CAST('2022-01-01' AS DATE)) AS cal_year, DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day, QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter, MONTH(CAST('2022-01-01' AS DATE)) AS cal_month, MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name, DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day, WEEK(CAST('2022-01-01' AS DATE)) AS cal_week, DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day, DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name UNION ALL -- Recursive member SELECT ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date, YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year, DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day, QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter, MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month, MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name, DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day, WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week, DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day, DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name FROM cte WHERE cal_date < CAST('2025-01-10' AS DATE)) select * from calender_dim;
BUT IN MYSQL, insert into syntax not working,"CREATE TABLE IF NOT EXISTS calendar_dim ( cal_date DATE, cal_year INT, cal_year_day INT, cal_quarter INT, cal_month INT, cal_month_name VARCHAR(20), cal_month_day INT, cal_week INT, cal_week_day INT, cal_day_name VARCHAR(20) ); -- Use the recursive CTE to generate data insert into calender_dim WITH RECURSIVE cte AS ( -- Anchor member SELECT CAST('2022-01-01' AS DATE) AS cal_date, YEAR(CAST('2022-01-01' AS DATE)) AS cal_year, DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day, QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter, MONTH(CAST('2022-01-01' AS DATE)) AS cal_month, MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name, DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day, WEEK(CAST('2022-01-01' AS DATE)) AS cal_week, DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day, DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name UNION ALL -- Recursive member SELECT ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date, YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year, DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day, QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter, MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month, MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name, DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day, WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week, DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day, DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name FROM cte WHERE cal_date < CAST('2025-01-10' AS DATE) ) select * from calender_dim;" this one is working but output record is showing zero. please help
CREATE TABLE IF NOT EXISTS calender_dim ( cal_date DATE, cal_year INT, cal_year_day INT, cal_quarter INT, cal_month INT, cal_month_name VARCHAR(20), cal_month_day INT, cal_week INT, cal_week_day INT, cal_day_name VARCHAR(20) ); -- Use the recursive CTE to generate data WITH recursive cte AS ( -- Anchor member SELECT CAST('2022-01-01' AS DATE) AS cal_date, YEAR(CAST('2022-01-01' AS DATE)) AS cal_year, DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day, QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter, MONTH(CAST('2022-01-01' AS DATE)) AS cal_month, MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name, DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day, WEEK(CAST('2022-01-01' AS DATE)) AS cal_week, DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day, DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name UNION ALL -- Recursive member SELECT ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date, YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year, DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day, QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter, MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month, MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name, DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day, WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week, DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day, DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name FROM cte WHERE cal_date < CAST('2025-01-10' AS DATE)) select * from cte; -- Just modified your query. This will work in mysql
I tried to make a calendar table in MySQL: SET cte_max_recursion_depth=30000; with recursive cte as (select cast('2000-01-01' as date) as cal_date ,year('2000-01-01') as cal_year ,dayofyear('2000-01-01') as cal_year_day ,quarter('2000-01-01') as cal_quarter ,month('2000-01-01') as cal_month ,monthname('2000-01-01') as cal_month_name ,day('2000-01-01') as cal_month_day ,week('2000-01-01') as cal_week ,weekday('2000-01-01') as cal_week_day ,dayname('2000-01-01') as cal_day_name union all select date_add(cal_date, interval 1 day) as cal_date, year(date_add(cal_date, interval 1 day)) as cal_year, dayofyear(date_add(cal_date, interval 1 day)) as cal_year_day, quarter(date_add(cal_date, interval 1 day)) as cal_quarter, month(date_add(cal_date, interval 1 day)) as cal_month, monthname(date_add(cal_date, interval 1 day)) as cal_month_name, day(date_add(cal_date, interval 1 day)) as cal_month_day, week(date_add(cal_date, interval 1 day)) as cal_week, weekday(date_add(cal_date, interval 1 day)) as cal_week_day, dayname(date_add(cal_date, interval 1 day)) as cal_day_name from cte where cal_date < cast('2050-12-31' as date) ) select row_number() over(order by cal_date asc) as rn, cal_date, cal_year, cal_year_day, cal_quarter, cal_month, cal_month_name, cal_month_day, cal_week, cal_week_day, cal_day_name from cte;
Ankit Bhai, boht barhya kaam kiya hai... 2 months se dhoondh raha tha. Itnay din se yeh video kidhar th bhayya. Thanks ❤
Thanks for creating the data engineering videos, I love your explanation. Good luck sir
Thank you for this video, I've converted it into Stored Procedure to make it more dynamic. Please find it below :
/*
The stored procedure sp_create_calendar_dim_table is
to generate a calendar dimension table (cal_dim_new) containing a range of dates between the specified start and end dates
as parameters.
*/
-- Creation of Sp : sp_create_calender_dim_table
CREATE PROCEDURE sp_create_calender_dim_table
@start_date date,
@end_date date
as
BEGIN
with recursive_cte as (
Select @start_date as cal_date
union all
Select dateadd(dd,1,cal_date) as cal_date
from recursive_cte
where cal_date < @end_date
)
Select row_number() over(order by (select null)) as id, cal_date,
datepart(year,cal_date) as cal_year,
datepart(dayofyear,cal_date) as cal_year_day,
datepart(quarter,cal_date) as cal_quarter,
datepart(month,cal_date) as cal_month,
datename(month,cal_date) as cal_month_name,
datepart(day,cal_date) as cal_month_day,
datepart(week,cal_date) as cal_week,
datepart(weekday,cal_date) as cal_week_day,
datename(weekday,cal_date) as cal_day_name
into cal_dim_new
from recursive_cte
option (maxrecursion 0)
END;
-- Execute Sp : sp_create_calender_dim_table
EXEC sp_create_calender_dim_table @start_date = '2000-01-01', @end_date = '2050-12-31'
-- Check the cal_dim_new table
Select * from cal_dim_new
So excited for the upcoming query questions related to this calendar dimension table.
you blown my mind. god bless another god of sql.
Great explaination🎉
Thank You Bro Super explanation. I am learning very good stuff from you
No more distraction.. Just stick to Ankit Bhansal YT channel for SQL & thanks me later 😅
absolute true
you are the man ... Always great content -:)
Very good Logic ❤❤❤
Can you explain me what is the use of in joining of two table or in self join?
Nice explanation dates functions Ankit bhaiya ❤❤🎉🎉
Hi Ankit, it is a great learning, I have been following you from the beginning, would you please implement day light saving logic in the where condition, ex: my views delivering the report every 1 hour to the business
What exactly do you want please explain
Thank you Ankit 🙏🏻
Thankyou it's really superb 👏👏
Welcome 😊
Please make a video on subquery.
I am not able to understand how to solve subqueries problem.
I tried a lot from different different resources but not able to solve it .
And if there is any cheat method for this then also please share.
Thankyou
S plz share
Sir is it correct according to you ?
WITH cte AS (
SELECT CAST('2000-01-01' AS date) AS cal_date
UNION ALL
SELECT DATEADD(DAY, 1, cal_date) AS cal_date
FROM cte
WHERE cal_date < CAST('2050-12-30' AS date)
)
SELECT
cal_date,
DATEPART(YEAR, cal_date) AS cal_year,
DATEPART(dayofyear, cal_date) AS cal_year_day,
DATEPART(quarter, cal_date) AS cal_quarter,
DATEPART(month, cal_date) AS cal_month,
DATENAME(month, cal_date) AS cal_month_name,
DATEPART(day, cal_date) AS cal_month_day,
DATEPART(week, cal_date) AS cal_week,
DATEPART(weekday, cal_date) AS cal_week_day,
DATENAME(weekday, cal_date) AS cal_day_name
INTO calendar_table
FROM cte
OPTION (MAXRECURSION 32676);
Hey Ankit are we considering leap year also in the calendar?
Yes absolutely
Hey Ankit,
Can you please help me with how to create this dimension table in mysql
I am done with all the script following your video but I got stuck at last point when we need to create the physical table.
This query is not working in mysql....please help.
SELECT row_number() over(order by cal_date asc) as id , * into calendar_dimension from cte ;
SET SESSION cte_max_recursion_depth = 1000000;
Thanks in advance
Use create table as instead of into
@@ankitbansal6 Hello Ankit, Can you help in correcting the mistake, below is the script I'm using in MYSQL CREATE TABLE IF NOT EXISTS calendar_dim (
cal_date DATE,
cal_year INT,
cal_year_day INT,
cal_quarter INT,
cal_month INT,
cal_month_name VARCHAR(20),
cal_month_day INT,
cal_week INT,
cal_week_day INT,
cal_day_name VARCHAR(20)
);
-- Use the recursive CTE to generate data
insert into calender_dim
WITH RECURSIVE cte AS (
-- Anchor member
SELECT
CAST('2022-01-01' AS DATE) AS cal_date,
YEAR(CAST('2022-01-01' AS DATE)) AS cal_year,
DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day,
QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter,
MONTH(CAST('2022-01-01' AS DATE)) AS cal_month,
MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name,
DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day,
WEEK(CAST('2022-01-01' AS DATE)) AS cal_week,
DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day,
DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name
UNION ALL
-- Recursive member
SELECT
ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date,
YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year,
DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day,
QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter,
MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month,
MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name,
DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day,
WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week,
DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day,
DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name
FROM cte
WHERE cal_date < CAST('2025-01-10' AS DATE)
)
select * from calender_dim; Getting zero record insert into calender_dim
@@JayPatel-wv4mz remove into at the end ..into calendardim ...
@@ankitbansal6 sir , If I remove the insert into statement then I'm not getting any record from calender_dim table. It is not inserting the value into calender_dim table and since we have created CTE so I cannot use that CTE outside the recursive table . see the script below. CREATE TABLE IF NOT EXISTS calendar_dim (
cal_date DATE,
cal_year INT,
cal_year_day INT,
cal_quarter INT,
cal_month INT,
cal_month_name VARCHAR(20),
cal_month_day INT,
cal_week INT,
cal_week_day INT,
cal_day_name VARCHAR(20)
);
-- Use the recursive CTE to generate data
insert into calender_dim
WITH RECURSIVE cte AS (
-- Anchor member
SELECT
CAST('2022-01-01' AS DATE) AS cal_date,
YEAR(CAST('2022-01-01' AS DATE)) AS cal_year,
DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day,
QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter,
MONTH(CAST('2022-01-01' AS DATE)) AS cal_month,
MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name,
DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day,
WEEK(CAST('2022-01-01' AS DATE)) AS cal_week,
DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day,
DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name
UNION ALL
-- Recursive member
SELECT
ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date,
YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year,
DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day,
QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter,
MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month,
MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name,
DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day,
WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week,
DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day,
DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name
FROM cte
WHERE cal_date < CAST('2025-01-10' AS DATE))
select * from calender_dim;
BUT IN MYSQL, insert into syntax not working,"CREATE TABLE IF NOT EXISTS calendar_dim (
cal_date DATE,
cal_year INT,
cal_year_day INT,
cal_quarter INT,
cal_month INT,
cal_month_name VARCHAR(20),
cal_month_day INT,
cal_week INT,
cal_week_day INT,
cal_day_name VARCHAR(20)
);
-- Use the recursive CTE to generate data
insert into calender_dim
WITH RECURSIVE cte AS (
-- Anchor member
SELECT
CAST('2022-01-01' AS DATE) AS cal_date,
YEAR(CAST('2022-01-01' AS DATE)) AS cal_year,
DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day,
QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter,
MONTH(CAST('2022-01-01' AS DATE)) AS cal_month,
MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name,
DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day,
WEEK(CAST('2022-01-01' AS DATE)) AS cal_week,
DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day,
DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name
UNION ALL
-- Recursive member
SELECT
ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date,
YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year,
DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day,
QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter,
MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month,
MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name,
DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day,
WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week,
DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day,
DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name
FROM cte
WHERE cal_date < CAST('2025-01-10' AS DATE)
)
select * from calender_dim;" this one is working but output record is showing zero. please help
Hey use below script for my sql @rituraj5249
CREATE TABLE IF NOT EXISTS calender_dim (
cal_date DATE,
cal_year INT,
cal_year_day INT,
cal_quarter INT,
cal_month INT,
cal_month_name VARCHAR(20),
cal_month_day INT,
cal_week INT,
cal_week_day INT,
cal_day_name VARCHAR(20)
);
-- Use the recursive CTE to generate data
WITH recursive cte AS (
-- Anchor member
SELECT
CAST('2022-01-01' AS DATE) AS cal_date,
YEAR(CAST('2022-01-01' AS DATE)) AS cal_year,
DAYOFYEAR(CAST('2022-01-01' AS DATE)) AS cal_year_day,
QUARTER(CAST('2022-01-01' AS DATE)) AS cal_quarter,
MONTH(CAST('2022-01-01' AS DATE)) AS cal_month,
MONTHNAME(CAST('2022-01-01' AS DATE)) AS cal_month_name,
DAY(CAST('2022-01-01' AS DATE)) AS cal_month_day,
WEEK(CAST('2022-01-01' AS DATE)) AS cal_week,
DAYOFWEEK(CAST('2022-01-01' AS DATE)) AS cal_week_day,
DAYNAME(CAST('2022-01-01' AS DATE)) AS cal_day_name
UNION ALL
-- Recursive member
SELECT
ADDDATE(cal_date, INTERVAL 1 DAY) AS cal_date,
YEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year,
DAYOFYEAR(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_year_day,
QUARTER(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_quarter,
MONTH(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month,
MONTHNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_name,
DAY(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_month_day,
WEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week,
DAYOFWEEK(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_week_day,
DAYNAME(ADDDATE(cal_date, INTERVAL 1 DAY)) AS cal_day_name
FROM cte
WHERE cal_date < CAST('2025-01-10' AS DATE))
select * from cte;
-- Just modified your query. This will work in mysql
Perfect explanation
Thank you 😊
I tried to make a calendar table in MySQL:
SET cte_max_recursion_depth=30000;
with recursive cte as
(select cast('2000-01-01' as date) as cal_date
,year('2000-01-01') as cal_year
,dayofyear('2000-01-01') as cal_year_day
,quarter('2000-01-01') as cal_quarter
,month('2000-01-01') as cal_month
,monthname('2000-01-01') as cal_month_name
,day('2000-01-01') as cal_month_day
,week('2000-01-01') as cal_week
,weekday('2000-01-01') as cal_week_day
,dayname('2000-01-01') as cal_day_name
union all
select date_add(cal_date, interval 1 day) as cal_date,
year(date_add(cal_date, interval 1 day)) as cal_year,
dayofyear(date_add(cal_date, interval 1 day)) as cal_year_day,
quarter(date_add(cal_date, interval 1 day)) as cal_quarter,
month(date_add(cal_date, interval 1 day)) as cal_month,
monthname(date_add(cal_date, interval 1 day)) as cal_month_name,
day(date_add(cal_date, interval 1 day)) as cal_month_day,
week(date_add(cal_date, interval 1 day)) as cal_week,
weekday(date_add(cal_date, interval 1 day)) as cal_week_day,
dayname(date_add(cal_date, interval 1 day)) as cal_day_name
from cte
where cal_date < cast('2050-12-31' as date)
)
select row_number() over(order by cal_date asc) as rn,
cal_date, cal_year, cal_year_day, cal_quarter, cal_month, cal_month_name, cal_month_day, cal_week, cal_week_day, cal_day_name
from cte;
how have you created dim table using this cte ?
@@shashanktiwari133 it's a recursive cte not just an ordinary cte. it works the same as you apply recursion in any function while coding.