SQL Tutorial: Working with calendar tables

14 Mar 2020 · 4 min
4:07

In this tutorial, the presenter explains the concept and construction of a calendar (date dimension) table in SQL Server and demonstrates how it simplifies date‑related queries. The talk begins by defining a calendar table as a static, wide table that stores a date key, the date itself, and supporting attributes. Column categories are outlined: general columns (date, day name, weekend flag), calendar‑year attributes (quarter, year), fiscal‑year attributes (fiscal year, fiscal quarter, fiscal day/week), and specialized columns such as holidays, lunar dates, or other business‑specific markers. The presenter advises building the table once, treating it as a warehouse dimension, and leveraging built‑in date functions or existing scripts rather than reinventing the wheel. Practical query examples illustrate how the table makes it easy to retrieve specific days—e.g., all Saturdays in April 2020—and to exclude dates that coincide with holidays like Passover or Easter. The tutorial then introduces the T‑SQL APPLY operator, describing it as a row‑by‑row function executor that often outperforms alternative methods. An example shows using APPLY to compute the fiscal year start (July 1) and then derive fiscal day‑of‑year and fiscal week‑of‑year, with a suggestion to encapsulate the fiscal‑year start logic in an ad‑hoc function to reduce repetition and errors. The session concludes by directing learners to hands‑on exercises.

Chapters

More from Richie

youtube.com

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

5mo ago
youtube.com

17.8% of the World Now Uses AI — And the Gap Is Widening | The Median #5

5mo ago
youtube.com

Create GPTs to Automate Go-To-Market Research

5mo ago
youtube.com

What Happened to Self-Driving Cars? #AI

5mo ago
youtube.com

Never Use AI For This #warning #AI #flights

5mo ago
youtube.com

Use AI Agents to Analyze Power Grid Optimization

5mo ago
youtube.com

This is how modern AI actually solves problems #AITrends #AI

5mo ago
youtube.com

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

5mo ago
youtube.com

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

5mo ago
youtube.com

Build an AI‑driven Dashboard

5mo ago
youtube.com

What’s Broken in Labor Markets Today #datascience

5mo ago
See all content