Time Series Analysis
Working with Dates and Times
Building dates
DECLARE
@SomeTime DATETIME2(7) = SYSUTCDATETIME();
-- Retrieve the year, month, and day
SELECT
Year(@SomeTime) AS TheYear,
Month(@SomeTime) AS TheMonth,
DAy(@SomeTime) AS TheDay;
DECLARE
@BerlinWallFalls DATETIME2(7) = '1989-11-09 23:49:36.2294852';
-- Fill in each date part
SELECT
DATEPART(year, @BerlinWallFalls) AS TheYear,
DATEPART(month, @BerlinWallFalls) AS TheMonth,
DATEPART(day, @BerlinWallFalls) AS TheDay,
DATEPART(dayofyear, @BerlinWallFalls) AS TheDayOfYear,
-- Day of week is WEEKDAY
DATEPART(WEEKDAY, @BerlinWallFalls) AS TheDayOfWeek,
DATEPART(week, @BerlinWallFalls) AS TheWeek,
DATEPART(second, @BerlinWallFalls) AS TheSecond,
DATEPART(nanosecond, @BerlinWallFalls) AS TheNanosecond;
DECLARE
@BerlinWallFalls DATETIME2(7) = '1989-11-09 23:49:36.2294852';
-- Fill in the function to show the name of each date part
SELECT
DATENAME(YEAR, @BerlinWallFalls) AS TheYear,
DATENAME(MONTH, @BerlinWallFalls) AS TheMonth,
DATENAME(DAY, @BerlinWallFalls) AS TheDay,
DATENAME(DAYOFYEAR, @BerlinWallFalls) AS TheDayOfYear,
-- Day of week is WEEKDAY
DATENAME(WEEKDAY, @BerlinWallFalls) AS TheDayOfWeek,
DATENAME(WEEK, @BerlinWallFalls) AS TheWeek,
DATENAME(SECOND, @BerlinWallFalls) AS TheSecond,
DATENAME(NANOSECOND, @BerlinWallFalls) AS TheNanosecond;
formatting dates for reporting
working with calender tables
Converting to Dates and Times
Building Dates from parts
Translating Date Strings
working with offsets
handling invalid Dates
Aggregating Time Series Data
Basic Aggregate function
Statistical aggreate functionality
Downsampling and upsampling date
Grouping by Rollup, cube, grouping sets
Answering Time Series Questions with Window Functions
using aggregate functions over windows
calculate running totals and moving averages
Working with LAG() and LEAD()
Finding maximum level of overlap
Wrapping up
Last updated