|
Hi Reader, Time-based questions come up constantly in business. "Show me bookings from the last 90 days." "How does this quarter compare to last quarter?" "What's the week-over-week trend?" And most analysts handle them the worst possible way: by hardcoding dates.
This works today. Next quarter, you have to remember to change the dates. You won't remember. And the report will silently show stale data until someone notices. Here are three date function patterns that make your time-based queries automatic. Pattern 1: Rolling Windows With INTERVAL"Show me the last N days/months/years" — this is probably the most common time-based request.
I ran this against the Summit Adventures database (the fake adventure tourism company I created to help people learn business analytics). The key line:
Common INTERVAL values:
Pattern 2: Grouping by Time Period With DATE_TRUNC"Show me revenue by month" or "bookings by week" — you need to group timestamps into periods.
DATE_TRUNC options:
Pattern 3: Comparing Time Periods"How did this month compare to last month?" — this combines date functions with the comparison techniques we covered a few weeks ago.
This query always compares the current month-to-date with the previous full month. No hardcoded dates. It updates automatically every time you run it. Putting It All Together: The Executive Dashboard QueryHere's how these patterns combine into a practical business report:
This gives your CEO everything in one query: current quarter performance, previous quarter comparison, and the percentage change. All automatic. Try This At Your JobTake a report you update manually (changing date ranges each time) and replace the hardcoded dates with CURRENT_DATE and INTERVAL. You'll never have to touch those dates again. Until next time, Brian P.S. Time-based analysis is the focus of Module 3 of SQL for Business Impact, where we use the Heartrate Monitor Blueprint to understand booking patterns over time. If you want to go deeper into trend analysis, seasonal patterns, and time-series queries, check it out at sqlforbusinessimpact.com. P.P.S. Do you have reports with hardcoded dates that break every month? Reply and tell me about them — I might feature the fix in a future email. I read every response. |
Learn to build analytics projects with SQL, Tableau, Excel, and Python. For data analysts looking to level up their career and complete beginners looking to get started. No fluff. No theory. Just step-by-step tutorials anyone can follow.
Hello Reader, This week's newsletter is a bit different. There's SQL in here, but the real topic is career strategy. Because one of the most common questions I get from readers is some version of: "I've been learning SQL for a few months. How do I prove I can actually do this job?" The honest answer: a portfolio of 2-3 projects that demonstrate business thinking, not just technical syntax. The problem is most portfolios I've reviewed look the same. A Kaggle competition. A tutorial from...
Hello Reader, A common analytics question that sounds simple but isn't: "Which customers have booked the same type of expedition more than once?" You can't answer this with a regular JOIN between two different tables. The information lives in one table. You need to compare rows within that table to find patterns. That's what a self-JOIN does. It joins a table to itself. It sounds unusual, but once you see the pattern, you'll recognize situations where it's exactly what you need. The Business...
Hello Reader, You've built a clean customer report. Locations, booking history, revenue by region. You send it off to whoever asked for it. Ten minutes later: "Why does this report show blank cells in the location column? And why do the totals not add up?" Welcome to the NULL problem. You're going to learn how to solve it once and for all today. In the Summit Adventures database (the fake adventure tourism company I created to help people learn business analytics), 78.2% of customers don't...