DP080 Lecture 5
DP080 Lecture 5
DP080 Lecture 5
GROUP BY and
Window functions
Module
Agenda OVER Clause and Window functions
● MIN() Syntax
● MAX() Syntax
● COUNT() Syntax
-- Returns the number of rows that matches a specified criteria
SELECT COUNT([DISTINCT] columns_name)
FROM table_name
● AVG() Syntax
-- Returns the average value of a numeric column
SELECT AVG(column_name)
FROM table_name
● SUM() Syntax
-- Returns the total sum of a numeric column
SELECT SUM(column_name)
FROM table_name
Aggregate functions – Practice
Exercise 1: Write a query to determine the number of products in the DimProduct table.
Exercise 2: Write a query using the DimProduct table that displays the minimum, maximum,
and average ListPrice of all Product
SELECT OrderDate
, MAX(SalesAmount) as highest_sales
, SUM(SalesAmount) as total_sales
FROM [FactInternetSales]
WHERE OrderDate >= '2011-01-01'
GROUP BY OrderDate
GROUP BY – Practice
Exercise 1: Write a query that displays the count of orders placed by each year for each
customer using the FactInternetsales table
Exercise : The company is about to run a loyalty scheme to retain customers having total
value of orders greater than 5000 USD per year. From FactInternetSales table, retrieve the
list of qualified customers and the corresponding year.
Lesson 2: Window Functions
RANK() OVER (
[PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC | DESC], ...)
● First, the PARTITION BY clause divides the rows of the result set partitions to which
the function is applied.
● Second, the ORDER BY clause specifies the logical sort order of the rows in each
a partition to which the function is applied
© Copyright Microsoft Corporation. All rights reserved.