-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindows Ranking Func..sql
More file actions
152 lines (135 loc) · 4.07 KB
/
Windows Ranking Func..sql
File metadata and controls
152 lines (135 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ==============================================================================
SQL Window Ranking Functions
-------------------------------------------------------------------------------
These functions allow you to rank and order rows within a result set
without the need for complex joins or subqueries. They enable you to assign
unique or non-unique rankings, group rows into buckets, and analyze data
distributions on ordered data.
Table of Contents:
1. ROW_NUMBER: Unique Rank, Does NOT handle Ties, No Gaps in Ranks
2. RANK: Shared Rank, Handles Ties, Gaps in Ranks
3. DENSE_RANK: Shared Rank, Handles Ties, No Gaps in Ranks
4. NTILE
5. CUME_DIST - CUMILITIVE DISTRIBUTION
6. PERCENT_RANK.
CUME_DIST:
- Formula: Position Nr / Number of Rows
- Inclusive (The current row is included)
PERCENT_RANK:
- Formula: (Position Nr - 1) / (Number of Rows - 1)
- Exclusive (The current row is excluded)
=================================================================================
*/
/* ============================================================
SQL WINDOW RANKING | ROW_NUMBER, RANK, DENSE_RANK
============================================================ */
SELECT
OrderID,
ProductID,
Sales,
ROW_NUMBER() OVER(ORDER BY Sales DESC) AS SalesRank_RowNumber,
RANK() OVER(ORDER BY Sales DESC) AS SalesRank_Rank,
DENSE_RANK() OVER(ORDER BY Sales DESC) AS SalesRank_DenseRank
FROM Sales.Orders;
/* TASK 2:
Use Case | Top-N Analysis: Find the Highest Sale for Each Product
*/
SELECT *
FROM(
SELECT
OrderID,
ProductID,
Sales,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY Sales DESC) AS [Rank By Product]
FROM Sales.Orders) AS T
WHERE [Rank By Product] = 1;
/* TASK 3:
Use Case | Bottom-N Analysis: Find the Lowest 2 Customers Based on Their Total Sales
*/
SELECT *
FROM (
SELECT
CustomerID,
SUM(Sales) AS TotalSales,
ROW_NUMBER() OVER (ORDER BY SUM(Sales)) AS RankCustomers
FROM Sales.Orders
GROUP BY CustomerID
) AS BottomCustomerSales
WHERE RankCustomers <= 2;
/* TASK 4:
Use Case | Assign Unique IDs to the Rows of the 'Order Archive'
*/
SELECT
ROW_NUMBER()OVER(ORDER BY OrderID) AS Unique_ID,
*
FROM Sales.OrdersArchive;
/* TASK 5:
Use Case | Identify Duplicates:
Identify Duplicate Rows in 'Order Archive' and return a clean result without any duplicates
*/
SELECT *
FROM(
SELECT
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY CreationTime) AS RN,
*
FROM Sales.OrdersArchive) AS T
WHERE RN > 1;
/* ============================================================
SQL WINDOW RANKING | NTILE
============================================================ */
/* TASK 6:
Divide Orders into Groups Based on Sales
*/
SELECT
OrderID,
ProductID,
Sales,
NTILE(1) OVER(ORDER BY Sales DESC) AS OneBucket,
NTILE(2) OVER(ORDER BY Sales DESC) AS TwoBucket,
NTILE(3) OVER(ORDER BY Sales DESC) AS ThreeBucket,
NTILE(4) OVER(ORDER BY Sales DESC) AS FourBucket
FROM Sales.Orders;
/* TASK 7:
Segment all Orders into 3 Categories: High, Medium, and Low Sales.
*/
SELECT
*,
CASE WHEN Bucket = 1 THEN 'HIGH'
WHEN Bucket = 2 THEN 'Medium'
WHEN Bucket = 3 THEN 'Low'
END SalesSegmentations
FROM
(SELECT
OrderID,
Sales,
NTILE(3) OVER(ORDER BY Sales DESC) AS Bucket
FROM Sales.Orders) AS B;
/* TASK 8:
Divide Orders into Groups for Processing
*/
SELECT
NTILE(2) OVER(ORDER BY OrderID)AS GFP,
*
FROM Sales.Orders;
/* ============================================================
SQL WINDOW RANKING | CUME_DIST
============================================================ */
/* TASK 9:
Find Products that Fall Within the Highest 40% of the Prices
*/
SELECT
*
FROM Sales.Products;
SELECT
Product,
Price,
DistRank,
CONCAT(DistRank * 100, '%') AS DistRankPerc
FROM (
SELECT
Product,
Price,
CUME_DIST() OVER (ORDER BY Price DESC) AS DistRank
FROM Sales.Products
) AS PriceDistribution
WHERE DistRank <= 0.4;