0% found this document useful (0 votes)
6 views

B7-MD01 HW AnswerKey

The document contains SQL queries demonstrating the use of window functions, specifically RANK, DENSE_RANK, and ROW_NUMBER, to rank various metrics in different tables. It includes examples for ranking currency rates, service grades, product prices, and employee sales quotas, as well as calculating average sales and order quantities for resellers and online customers. Additionally, it showcases how to aggregate total sales amounts by year and identify the top resellers by sales territory.

Uploaded by

k61.2214750012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

B7-MD01 HW AnswerKey

The document contains SQL queries demonstrating the use of window functions, specifically RANK, DENSE_RANK, and ROW_NUMBER, to rank various metrics in different tables. It includes examples for ranking currency rates, service grades, product prices, and employee sales quotas, as well as calculating average sales and order quantities for resellers and online customers. Additionally, it showcases how to aggregate total sales amounts by year and identify the top resellers by sales territory.

Uploaded by

k61.2214750012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

USE AdventureWorksDW2022

/*
Phần 4: Sử dụng WINDOW FUNCTION
*/
--Câu 1: Nêu sự khác nhau giữa RANK, DENSE_RANK và ROW_NUMBER
--https://datapot.vn/3-ham-xep-hang-trong-sql-row-number-rank-dense-rank/
--Câu 2: Xếp hạng EndOfDayRate theo từng CurrencyKey trong bảng FactCurrencyRate
SELECT CurrencyKey,
EndOFDayRate,
DENSE_RANK() OVER (
PARTITION BY CurrencyKey
ORDER BY EndOfDayRate DESC
) AS RankEndOFDayRate
FROM FactCurrencyRate
--Câu 3: Xếp hạng ServiceGrade theo từng WageType và Shift trong bảng FactCallCenter
SELECT WageType,
Shift,
ServiceGrade,
DENSE_RANK() OVER (
PARTITION BY WageType,
Shift
ORDER BY ServiceGrade
) AS RankOfServiceGrade
FROM FactCallCenter
--Câu 4: Xếp hạng ListPrice trong bảng DimProduct theo từng EnglishProductSubcategoryName
--trong bảng DimProductSubcategory
SELECT ProductKey,
EnglishProductSubcategoryName,
ListPrice,
DENSE_RANK() OVER (
PARTITION BY EnglishProductSubcategoryName
ORDER BY ListPrice
) AS RankListPrice
FROM (
DimProduct
LEFT JOIN DimProductSubcategory ON DimProduct.ProductSubcategoryKey =
DimProductSubcategory.ProductSubcategoryKey
)
--Câu 5 (nâng cao): Trả về TOP 3 EmployeeKey có target cao nhất với từng năm trong bảng
--FactSalesQuota
WITH NewFactSalesQuota AS (
SELECT CalendarYear,
EmployeeKey,
sum(SalesAmountQuota) TotalSalesAmountQuota,
DENSE_RANK() OVER (
PARTITION BY CalendarYear
ORDER BY sum(SalesAmountQuota)
) AS RankSalesAmountQuota
FROM FactSalesQuota
Group By EmployeeKey,
CalendarYear
)
SELECT *
FROM NewFactSalesQuota
WHERE RankSalesAmountQuota <= 3
--Phần 5: Sử dụng truy vấn lồng với CTE
/*
Hãy viết câu truy vấn cho biết các thông tin sau:
- Trung bình một reseller chi tiêu bao nhiêu tiền ?
- Trung bình một reseller mua bao nhiêu đơn hàng ?
*/
WITH TotalSales AS (
SELECT ResellerKey,
SUM(SalesAmount) AS 'Revenue',
SUM(OrderQuantity) AS 'OrderQty'
FROM FactResellerSales
GROUP BY ResellerKey
)
SELECT AVG([Revenue]) AS 'AvgSalesPerCus',
AVG([OrderQty]) AS 'AvgOrderQty'
FROM TotalSales;
/*
- Trung bình một khách hàng online chi tiêu bao nhiêu tiền ?
- Trung bình một khách hàng online mua bao nhiêu đơn ?
*/
WITH TotalSales AS (
SELECT CustomerKey,
SUM(SalesAmount) AS 'Revenue',
SUM(OrderQuantity) As 'OrderQty'
FROM FactInternetSales
GROUP BY CustomerKey
)
SELECT AVG([Revenue]) AS 'AvgSalesPerCus',
AVG([OrderQty]) AS 'AvgOrderQty'
FROM TotalSales;
--Nâng cao: hãy thể hiện yêu câu trên thành một bảng duy nhất như dưới đây (tham khảo
UNION

UNION ALL
) WITH TotalSales AS (
SELECT CustomerKey,
SUM(SalesAmount) AS 'Revenue',
SUM(OrderQuantity) As 'OrderQty',
'Online' AS 'Category'
FROM FactInternetSales
GROUP BY CustomerKey
UNION ALL
SELECT ResellerKey,
SUM(SalesAmount) AS 'Revenue',
SUM(OrderQuantity) AS 'OrderQty',
'Reseller' AS 'Category'
FROM FactResellerSales
GROUP BY ResellerKey
)
SELECT Category,
AVG([Revenue]) AS 'AvgSalesPerCus',
AVG([OrderQty]) AS 'AvgOrderQty'
FROM TotalSales
GROUP BY Category
GO
--Câu 2: Viết truy vấn trả về bảng kết quả như dưới
--YEAR Total SalesAmount (Internet) Total SalesAmount (Reseller)
WITH TotalSalesAmount AS (
SELECT YEAR(OrderDate) AS YearOrderDate,
SUM(SalesAmount) AS TotalSalesAmountInternet,
0 AS TotalSalesAmountReseller
FROM FactInternetSales
GROUP BY YEAR(OrderDate)
UNION
SELECT YEAR(OrderDate) AS YearOrderDate,
0 AS TotalSalesAmountInternet,
SUM(SalesAmount) AS TotalSalesAmountReseller
FROM FactResellerSales
GROUP BY YEAR(OrderDate)
)
SELECT YearOrderDate,
SUM(TotalSalesAmountInternet) AS 'Total SalesAmount (Internet)',
SUM(TotalSalesAmountReseller) AS 'Total SalesAmount (Reseller)'
FROM TotalSalesAmount
GROUP BY YearOrderDate
GO
--Câu 3: Viết truy vấn trả về top 3 Reseller có tổng doanh số (SalesAmount) cao nhất với
--từng SalesTerritoryRegion (trong bảng DimSalesTerritory)
WITH RankReseller AS(
SELECT SalesTerritoryRegion,
ResellerKey,
SUM(SalesAmount) AS TotalSalesAmount,
DENSE_RANK() OVER (
PARTITION BY SalesTerritoryRegion
ORDER BY SUM(SalesAmount) DESC
) AS RankTotalSalesAmount
FROM (
FactResellerSales
LEFT JOIN DimSalesTerritory ON
FactResellerSales.SalesTerritoryKey = DimSalesTerritory.SalesTerritoryKey
)
GROUP BY SalesTerritoryRegion,
ResellerKey
)
SELECT *
FROM RankReseller
WHERE RankTotalSalesAmount <= 3

You might also like