Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Total revenue for multiple periods
Calculating deltas
Creating "revenue in quarters" reports
Summary

Instruction

First, let's see how our revenue has changed over time. To do so, we'll compare revenue values between consecutive years, months, or any other time periods. Such reports are commonly called year-to-year, quarter-to-quarter, and month-to-month reports. In Microsoft SQL Server, we can use the DATEPART() function to create such reports. Take a look:

SELECT
  DATEPART(Year, OrderDate) AS RevenueYear,
  SUM(Amount) AS TotalRevenue 
FROM Orders
GROUP BY DATEPART(Year, OrderDate)
ORDER BY DATEPART(Year, OrderDate);

And the result:

RevenueYear TotalRevenue
2016 208083.98
2017 617085.21
2018 440623.90

The DATEPART(time unit, date) function takes two parameters: a time unit (such as Year, Quarter, Month, etc.) and a date or time column. As the name suggests, the function returns the specified part of a given date.

In our example, DATEPART(Year, OrderDate) returns the year when the order was placed (2016, 2017, or 2018). As you can see, we also use the DATEPART() function to group all the orders and sum the revenue values for each year.

Note that we also added an ORDER BY clause to make sure the revenue values are shown in chronological order.

Exercise

For each year, show the total revenue from all orders shipped to the USA. Show two columns: RevenueYear and TotalRevenueUSA.

Order the rows by year.

Stuck? Here's a hint!

Modify the query from the explanation. Add a WHERE clause with ShipCountry = N'USA'.