Lecture 11 DMS
Lecture 11 DMS
1
12/5/2023
Lecture Agenda
2
12/5/2023
● MIN() Syntax
● MAX() Syntax
● COUNT() Syntax
3
12/5/2023
● Try:
SELECT OrderDate
, MAX(SalesAmount) as highest_sales
, SUM(SalesAmount) as total_sales
FROM [FactInternetSales]
WHERE OrderDate >= '2011-01-01'
GROUP BY OrderDate
4
12/5/2023
Exercise 1 : Write a query that displays the count of orders placed by each year for each customer
using the FactInternetsales table
Exercise 2: Write a query using DimProduct and DimProductSubcategory tables to display number of
product in each SubcategoryName (English)
10
5
12/5/2023
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.
11
12
6
12/5/2023
ROW_NUMBER ()
OVER ( [ PARTITION BY value_expression , ... [ n ] ]
order_by_clause )
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
13
14
7
12/5/2023
15
Window Functions
16
8
12/5/2023
SELECT
DateKey
, LEAD(DateKey) OVER (Order By DateKey DESC) as LEAD_Date
, LAG(DateKey) OVER (Order By DateKey DESC) as LAG_Date
FROM DimDate
17
SELECT
CalendarYear AS Year
, CalendarQuarter AS Quarter
, EmployeeKey
, SalesAmountQuota AS SalesQuota
, LEAD(SalesAmountQuota,1,0) OVER (PARTITION BY Employeekey ORDER BY CalendarYear, CalendarQuarter) AS NextQuota
, SalesAmountQuota - LEAD(SalesAmountQuota,1,0)
OVER (PARTITION BY Employeekey ORDER BY CalendarYear, CalendarQuarter) AS Diff
, SUM(SalesAmountQuota) OVER (PARTITION BY CalendarYear, CalendarQuarter) AS Total_Quater_Quota
FROM Quota
ORDER BY EmployeeKey, CalendarYear, CalendarQuarter;
18
9
12/5/2023
19
Sorting Results
20
10
12/5/2023
21
22
11
12/5/2023
23
Removing Duplicates
City CountryRegion
Aurora Canada
• SELECT ALL Barrie Canada
• Default behavior includes duplicates Brampton Canada
Brossard Canada
SELECT City, CountryRegion Brossard Canada
Burnaby Canada
FROM SalesLT.Address Burnaby Canada
ORDER BY CountryRegion, City; Burnaby Canada
Calgary Canada
Calgary Canada
• SELECT DISTINCT
• Removes duplicates City CountryRegion
Aurora Canada
SELECT DISTINCT City, CountryRegion Barrie Canada
FROM SalesLT.Address Brampton Canada
ORDER BY CountryRegion, City; Brossard Canada
Burnaby Canada
Calgary Canada
24
12
12/5/2023
• WHERE Statement
25
= Equal
<> Or != Not equal
Comparison operators > Greater than
< Less than
>= Greater than or equal
<= Less than or equal
AND Return records that meet all the conditions separated by AND in WHERE clause.
Logical operators OR Return records that meet any of the conditions separated by OR in WHERE clause.
NOT Return records that do not satisfy any of the conditions in WHERE clause.
[NOT] BETWEEN Returns values [NOT] within a given range
[NOT] IN Specify multiple values in a WHERE clause (a shorthand for multiple OR conditions)
SQL operators IS [NOT] NULL Returns records having [NOT] NULL values in the given fileds.
[NOT] LIKE Returns records that [DO NOT] match a specified pattern in a column.
26
13
12/5/2023
Module Review
You write a Transact-SQL query to list the available sizes for products. Each individual size should be listed only once. Which
query should you use?
❑ SELECT Size FROM Production.Product;
❑ SELECT DISTINCT Size FROM Production.Product;
❑ SELECT ALL Size FROM Production.Product;
You must return the InvoiceNo and TotalDue columns from the Sales.Invoice table in decreasing order of TotalDue value.
Which query should you use?
❑ SELECT * FROM Sales.Invoice ORDER BY TotalDue, InvoiceNo;
❑ SELECT InvoiceNo, TotalDue FROM Sales.Invoice ORDER BY TotalDue DESC;
❑ SELECT TotalDue AS DESC, InvoiceNo FROM Sales.Invoice;
Complete this query to return only products that have a Category value of 2 or 4:
SELECT Name, Price FROM Production.Product
❑ ORDER BY Category;
❑ WHERE Category BETWEEN 2 AND 4;
❑ WHERE Category IN (2, 4);
27
28
14
12/5/2023
THANK YOU !
29
15