SQL Interview & Case Lab
Back to Interview SQL Patterns

Custom Date Buckets — Fiscal Weeks and Rolling Windows

When DATE_TRUNC isn't enough — custom fiscal calendars, rolling buckets, ISO weeks. FIND_VIDEO: search 'SQL date bucketing fiscal calendar' — recommended channel: EverSQL. Aim for 10 min or under.

9 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Introduction & Scope — The instructor outlines the goal of calculating fiscal years via CASE expressions and user-defined functions.
  2. Calendar vs. Fiscal Year — The timeline difference between standard calendar years and custom July-to-June fiscal cycles is explained.
  3. Logic Definition — The instructor introduces sample test dates (April and September) and the conditional month-checking strategy.
  4. Writing the CASE Statement — The SQL CASE expression is constructed to conditionally increment the calendar year based on the month value.
  5. Testing Date Scenarios — The query is tested against April 2020 (FY 2020) and September 2020 (FY 2021) to verify calculation accuracy.
  6. Encapsulating with UDFs — The instructor demonstrates how to wrap the CASE logic inside a reusable scalar function for cleaner SQL queries.
PDF notes

Frequently asked questions

Why does September 2020 fall under Fiscal Year 2021 in a July-June cycle?

Because the fiscal period starting in July 2020 runs through June 2021, and fiscal years take the year of the final period.

Can a fiscal year start on any arbitrary month?

Yes, fiscal years can start on any month as long as they span 12 consecutive months based on organizational accounting rules.

When should I use a CASE statement instead of a User-Defined Function?

Use a one-off CASE statement for single queries or high-scale data warehouse queries to avoid UDF execution overhead.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.