-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindows Aggregate Functions.sql
More file actions
224 lines (202 loc) · 5.79 KB
/
Windows Aggregate Functions.sql
File metadata and controls
224 lines (202 loc) · 5.79 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* ==============================================================================
SQL Window Aggregate Functions
-------------------------------------------------------------------------------
These functions allow you to perform aggregate calculations over a set
of rows without the need for complex subqueries. They enable you to compute
counts, sums, averages, minimums, and maximums while still retaining access
to individual row details.
Table of Contents:
1. COUNT
2. SUM
3. AVG
4. MAX / MIN
5. ROLLING SUM & AVERAGE Use Case
===============================================================================
*/
/* ============================================================
SQL WINDOW AGGREGATION | COUNT
============================================================ */
/* TASK 1:
Find the Total Number of Orders and the Total Number of Orders for Each Customer
*/
SELECT
OrderID,
OrderDate,
CustomerID,
COUNT(*) OVER() AS TotalOrders,
COUNT(*) OVER(PARTITION BY CustomerID) AS OrdersByCustomers
FROM Sales.Orders
/* TASK 2:
- Find the Total Number of Customers
- Find the Total Number of Scores for Customers
- Find the Total Number of Countries
*/
SELECT
*,
COUNT(*) OVER () AS TotalCustomersStar,
COUNT(1) OVER () AS TotalCustomersOne,
COUNT(Score) OVER() AS TotalScores,
COUNT(Country) OVER() AS TotalCountries
FROM Sales.Customers
/* TASK 3:
Check whether the table 'OrdersArchive' contains any duplicate rows
*/
SELECT
*
FROM (
SELECT
*,
COUNT(*) OVER(PARTITION BY OrderID) AS CheckDuplicates
FROM Sales.OrdersArchive
) t
WHERE CheckDuplicates > 1
/* ============================================================
SQL WINDOW AGGREGATION | SUM
============================================================ */
/* TASK 4:
- Find the Total Sales Across All Orders
- Find the Total Sales for Each Product
*/
SELECT
OrderID,
OrderDate,
Sales,
ProductID,
SUM(Sales) OVER () AS TotalSales,
SUM(Sales) OVER (PARTITION BY ProductID) AS SalesByProduct
FROM Sales.Orders
/* TASK 5:
Find the Percentage Contribution of Each Product's Sales to the Total Sales
*/
SELECT
OrderID,
ProductID,
Sales,
SUM(Sales) OVER () AS TotalSales,
ROUND(CAST(Sales AS FLOAT) / SUM(Sales) OVER () * 100, 2) AS PercentageOfTotal
FROM Sales.Orders
/* ============================================================
SQL WINDOW AGGREGATION | AVG
============================================================ */
/* TASK 6:
- Find the Average Sales Across All Orders
- Find the Average Sales for Each Product
*/
SELECT
OrderID,
OrderDate,
Sales,
ProductID,
AVG(Sales) OVER () AS AvgSales,
AVG(Sales) OVER (PARTITION BY ProductID) AS AvgSalesByProduct
FROM Sales.Orders
/* TASK 7:
Find the Average Scores of Customers
*/
SELECT
CustomerID,
LastName,
Score,
COALESCE(Score, 0) AS CustomerScore,
AVG(Score) OVER () AS AvgScore,
AVG(COALESCE(Score, 0)) OVER () AS AvgScoreWithoutNull
FROM Sales.Customers
/* TASK 8:
Find all orders where Sales exceed the average Sales across all orders
*/
SELECT
*
FROM (
SELECT
OrderID,
ProductID,
Sales,
AVG(Sales) OVER () AS Avg_Sales
FROM Sales.Orders
) t
WHERE Sales > Avg_Sales
/* ============================================================
SQL WINDOW AGGREGATION | MAX / MIN
============================================================ */
/* TASK 9:
Find the Highest and Lowest Sales across all orders
*/
SELECT
MIN(Sales) AS MinSales,
MAX(Sales) AS MaxSales
FROM Sales.Orders
/* TASK 10:
Find the Lowest Sales across all orders and by Product
*/
SELECT
OrderID,
ProductID,
OrderDate,
Sales,
MIN(Sales) OVER () AS LowestSales,
MIN(Sales) OVER (PARTITION BY ProductID) AS LowestSalesByProduct
FROM Sales.Orders
/* TASK 11:
Show the employees who have the highest salaries
*/
SELECT *
FROM (
SELECT *,
MAX(Salary) OVER() AS HighestSalary
FROM Sales.Employees
) t
WHERE Salary = HighestSalary
/* TASK 12:
Find the deviation of each Sale from the minimum and maximum Sales
*/
SELECT
OrderID,
OrderDate,
ProductID,
Sales,
MAX(Sales) OVER () AS HighestSales,
MIN(Sales) OVER () AS LowestSales,
Sales - MIN(Sales) OVER () AS DeviationFromMin,
MAX(Sales) OVER () - Sales AS DeviationFromMax
FROM Sales.Orders
/* ============================================================
Use Case | ROLLING SUM & AVERAGE
============================================================ */
/* TASK 13:
Calculate the moving average of Sales for each Product over time
*/
SELECT
OrderID,
ProductID,
OrderDate,
Sales,
AVG(Sales) OVER (PARTITION BY ProductID) AS AvgByProduct,
AVG(Sales) OVER (PARTITION BY ProductID ORDER BY OrderDate) AS MovingAvg
FROM Sales.Orders
/* TASK 14:
Calculate the moving average of Sales for each Product over time,
including only the next order
*/
SELECT
OrderID,
ProductID,
OrderDate,
Sales,
AVG(Sales) OVER (PARTITION BY ProductID ORDER BY OrderDate ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS RollingAvg
FROM Sales.Orders;
SELECT
OrderID,
ProductID,
OrderDate,
Sales,
AVG(Sales) OVER (PARTITION BY ProductID ORDER BY OrderDate ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS RollingAvg
FROM Sales.Orders;
/* TASK 14:
Rank Customers bases on their total Sales
*/
SELECT
CustomerID,
SUM(Sales) TotalSals,
RANK() Over(ORDER BY SUM(Sales)DESC) RankCustomers
From Sales.Orders
GROUP BY CustomerID;