Time Series Analysis

Working with Dates and Times

Building dates

Execises 🧮

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;

Dealing with leap years

Rounding dates SQL Server does not have an intuitive way to round down to the month, hour, or minute. You can, however, combine the DATEADD() and DATEDIFF() functions to perform this rounding. o round the date 1914-08-16 down to the year, we would call DATEADD(YEAR, DATEDIFF(YEAR, 0, '1914-08-16'), 0). To round that date down to the month, we would call DATEADD(MONTH, DATEDIFF(MONTH, 0, '1914-08-16'), 0). This works for several other date parts as well.

formatting dates for reporting

sql server has three formatting functions: CAST() FORMAT() CONVERT()

using the Convert(): it takes datatype, input, optional style FORMAT() is single threaded: (input, format code, optional culture)

working with calender tables

also knows as data dimensions calender table: is a table that stores dates for easy retrieval this is a table you build once and never update (nearly) Afte building the table, you can easily retrieve data from it

Apply() opertor: it excuete a function for each row on the result set, it simplifed calculations look at the slides, very important

we can use Calendar table to join other tables 🏓 :

Converting to Dates and Times

I this chapter we will learn how to create dates

Building Dates from parts

SQL Server has six function to build dates and times from component parts "the from parts series" look at the slides

If any of your input is null, the result will be null

exercise: Build data that Neil armstrong landed the moon at [date]

Translating Date Strings

working with offsets

handling invalid Dates

Aggregating Time Series Data

Basic Aggregate function

Exercise

Statistical aggreate functionality

Exercise

Downsampling and upsampling date

Exercise

Grouping by Rollup, cube, grouping sets

Exercise

Answering Time Series Questions with Window Functions

using aggregate functions over windows

Exercise

calculate running totals and moving averages

Exercise

Working with LAG() and LEAD()

Exercise

Finding maximum level of overlap

Exercise

Wrapping up

Last updated