I’m always trying different programming exercises in order to learn and grow as a Developer. One of my favorite learning exercises is porting over from one SQL dialect to another, as they all have their own individual features. Having to hack together or mirror non-existent functionality really challenges my thinking, therefore enabling growth and improvement in my query skills. In this post, I share reproducing the same query results using MySQL for queries I first learned of/discovered that were covered using Oracle SQL and specific implementation features…
Self-Promotion:
If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!
The Newsletter for PHP and MySQL Developers
Credit and Sources
I found great inspiration, learning, understanding, and the idea for this blog post based on this YouTube video:
Sample Data
The final query results we want are based on the present data in a table, ‘important_events’ having this fictisious data:
SELECT * FROM important_events;
Notice there are 5 random dates from the same month and year, with an event name and event comment. The goal is to provide results for every day of the entire month, including those days that do not have a row in table ‘important_events’.
Are you a Medium member? If so, receive an email notification each time I publish a blog post if you prefer the Medium platform. Not a member? No worries! Use my sign-up link (I will get a commission at no extra cost to you) and join. I really enjoy reading all the great content there and I know you will too!!!
The final results should be as those shown below:
Information needed
The information we need in order to retrieve the query results are:
The first day of the month based on the month value in the column ‘event_date’ The last day of the month based on the month value in the column ‘event_date’ The total number of days in the month based on the month value in the column ‘event_date’
In determining the first and last day of the respective month, we should use the minimum and maximum ‘event_date’ values currently stored in the ‘important_events’ table. We can easily get these values using both the MIN() and MAX() aggregate functions targeting the ‘event_date’ column:
SELECT MIN(event_date) AS min_date, MAX(event_date) AS max_date
FROM important_events;
Using the minimum and maximum date values, we calculate the first and last day of the month and the number of total days in the target month using specific MySQL Date functions. That query might look as the next example:
SELECT
DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY) AS mnth_first_day,
LAST_DAY(MAX(event_date)) AS mnth_last_day,
LAST_DAY(MAX(event_date)) – DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY) + 1 AS days_diff
FROM important_events;
MySQL does have a native LAST_DAY() date function we use to get the last day of the month. However, there is no FIRST_DAY() date function in MySQL (at the time of writing) so we have to do a little extra work and calculate it ourselves using the expression:
DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY)
If you would like to support my blog and content, please consider tossing some spare change in my Tip Jar. Thank you so much!
Recursive Common Table Expression
Oracle SQL has a CONNECT BY clause but there is no such native clause like it in MySQL as of the current writing. To replicate this functionality, for the purposes of this particular query, we can use a Recursive Common Table Expression (CTE).
For easier handling, I’ll store the number of days of the month in a session variable using the SET keyword:
SET @num_days := ”;
SELECT
LAST_DAY(MAX(event_date)) – DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY) + 1 INTO @num_days
FROM important_events;
SELECT @num_days;
I can then use the ‘@num_days’ variable in a recursive CTE as shown below:
The ‘n_dte’ CTE returns an incrementing set of INTEGER values equal to the total number of days in the target month (essentially 1 row for each day):
We can now add each individual incrementing INTEGER value to the 1st day of the month date value and return a calendar day for each day of the month. The query to produce this information might look like the below example:
SELECT
DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY) AS mnth_first_day,
n,
DATE_ADD(DATE_ADD(MIN(event_date), INTERVAL – DAY(MIN(event_date)) + 1 DAY), INTERVAL n – 1 DAY) AS all_days_in_mnth
FROM important_events
JOIN n_dte
group by n
ORDER BY n ASC;
The Newsletter for PHP and MySQL Developers
I’ve constrained the output rows for better readability shown in the below screenshot:
Multiple Common Table Expressions
We can have multiple CTE’s in the same WITH clause by separating them with commas, so I’ll include this query as its own CTE in addition to the already established, ‘n_dte’:
Together, our 2 CTE’s look like this:
Multiple CTE’s are the foundation of our query.
LEFT JOIN – Final Results Set
Now we simply need to LEFT JOIN the ‘all_mnth_days’ CTE to the ‘important_events’ table on the 2 date columns:
SELECT
amd.all_days_in_mnth AS event_date, IFNULL(ie.event_name, ‘No Event’) AS event_name, IFNULL(ie.event_comment, ‘No Event Comment’) AS event_comment
FROM all_mnth_days AS amd
LEFT JOIN important_events AS ie
ON ie.event_date = amd.all_days_in_mnth;
And we have the desired, final query results:
I won’t lay claim that this is the best, or only, or most performant way to solve this particular query in MySQL. Only that this is how I figured out how to retrieve this particular data. I’d love to know your thoughts, suggestions, and feedback. If you would’ve used a different approach or other MySQL functionality, please share in the comments below so that myself and other readers can learn and broaden our own query chops.
If you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate blog posts and are much appreciated. Thank you for reading!
Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.
To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.
The Newsletter for PHP and MySQL Developers
How can I help you?
Are you thinking of starting up a blog? I use WordPress for my blog. Let’s both save money on the plans offered. Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.Grab a free pack of mobile Creator wallpapers.Just getting started or wanting to learn MySQL? Find out about my premium blog posts and MySQL Beginner Series here.
Disclosure: Some of the services and products links in this post are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The post Fill in missing Date ranges using MySQL appeared first on Digital Owl’s Prose.