Loading lesson...
Temporal surgery: EXTRACT, DATE_TRUNC
Temporal surgery: EXTRACT, DATE_TRUNC
Topics covered: EXTRACT(), Date Part Constants, DATE_DIFF(), Unix Time Conversion, Grouping by Time Parts
Break apart a date into its calendar components: Time Component Extraction Calendar Extractions Practical Applications Filtering by Components How would you group revenue by year and quarter? Best Practices When to Use EXTRACT() Companies use these patterns to uncover time-of-day trends that drive product decisions.
Additional Date Parts Extracting Multiple Parts CURRENT_TIMESTAMP with EXTRACT This query extracts the current hour and classifies the time of day. The pattern is useful for dashboards that display time-aware greetings or adjust behavior based on business hours. Extracting DOW (day of week) is useful for detecting whether demand differs between weekdays and weekends. Grouping by DOW across a full year reveals structural patterns in consumer behavior.
Measuring Durations Response Time Analysis This query calculates response time in minutes and categorizes performance. Sub-hour granularity is critical for support SLAs where 15 minutes versus 45 minutes makes a significant difference. Aggregating Durations This query calculates session statistics per user: how many sessions, average duration, and total time spent. These metrics are fundamental for user engagement analysis. What unit should you measure in for different use cases? DATE_DIFF() Use
Unix time (also called epoch time or POSIX time) represents time as a single integer: the number of seconds since January 1, 1970 00:00:00 UTC. This representation is compact, timezone-independent, and universally understood by computing systems. Understanding Unix Time The Unix epoch (time zero) is January 1, 1970. Every second adds 1 to the counter. 1.7 billion seconds have passed since 1970, bringing us to 2024. This simple counting system enables easy time arithmetic. Converting from Unix Ti
Group transactions by year and month to see monthly trends: Hourly Traffic Patterns Group events by hour to find peak activity times: Hours 10-11 show peak traffic. This pattern reveals when users are most active, informing capacity planning and marketing schedules. Filtering by Time Parts