Creating and Using Calendar Tables Edward Pollack Creating and Using Calendar Tables
Agenda Making Date Calculations Fast and Efficient What is a calendar table? Simplifying code. Improving maintainability. Performance optimization. Demos Questions?
What is a Calendar Table? Table of dates and date components. Can contain large numbers of metrics. Eliminates date math on-the-fly. Eliminates ad-hoc date-math columns in tables. Used in: Analytics Reporting Anywhere you are sick of…
…this…
Calendar Tables: A Quick Glance Demo Calendar Tables: A Quick Glance
What Can go in a Calendar Table? Any date-based metric you can dream of. Date parts. Relative location in the calendar. String representations. Components of week, month, quarter, year, etc… Weekdays, business days, holidays, holiday seasons. Alternate calendars, such as fiscal, 4-5-4, etc…
Creating a Calendar Table Demo Creating a Calendar Table
Simplifying Code Remove frequent date math from any part of a query. Make TSQL more readable. Reduce mistakes and improve quality.
Demo Simplifying Code
Simplifying Code (cont.) Store very complex calculations for future use. Can include date-based business logic Easter, lunar holidays, alternate calendars, etc… Putting this code into TSQL is messy and error-prone.
Demo Determining Easter
Improving Maintainability Date definitions can be maintained in one place. Reduces repeated code. Creates authoritative source for date-based metrics. No need to reinvent the wheel. Reduce mistakes, increase reliability of code.
Performance Optimization Calendar table rarely/never changes. Calendar table can be heavily indexed. Remove messy functions and date math from filters, aggregates, and ordering. Focuses seeks/scans on the calendar table. Removes complexity from queries on large tables.
Demo More Demos!
Conclusion Calendar tables can: …and they are extremely inexpensive! Simplify code and make it more readable. Improve performance. Improve maintainability. Standardize date-based business logic. Help generate date ranges for processing. Provide a calendar data source in reporting. …and they are extremely inexpensive!
Questions???
Contact Info & Links Ed Pollack ed7@alum.rpi.edu @EdwardPollack SQL Shack SQL Server Central SQL Saturday Albany (2019) Thank you!!!