Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dealing with dates
Working with time data
Date and time date
Extracting dates and times
25. Using the DATEPART() function in grouping
Doing arithmetic with dates
Converting date and time data
Building date and time data from parts
Summary and review

Instruction

Nice! You can also use the DATEPART() function to group data. Let's analyze the example below:

SELECT
  AVG(Delay) AS AvgDelay,
  DATEPART(year, Date) AS Year,
  DATEPART(quarter, Date) AS Quarter
FROM Flight
GROUP BY DATEPART(year, Date),
  DATEPART(quarter, Date);

This query calculates the average delay for all flights and groups the calculation results by quarter.

Let's practice using DATEPART() with grouping.

Exercise

Help the manager prepare a report about the routes that PerfectAir flies. Show the average delay for each hour of arrival time. Name the columns Hour and AvgDelay.