-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.3.4_sql_window_functions.sql
214 lines (180 loc) · 6.61 KB
/
9.3.4_sql_window_functions.sql
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
USE Chinook;
-- 1. Rank the customers by total sales
SELECT
i.customerid,
c.firstname,
c.lastname,
SUM(i.total) as total_sales,
RANK() OVER (ORDER BY SUM(i.total) DESC) as sales_rank
FROM
invoice i
JOIN customer c
ON i.customerid = c.customerid
GROUP BY customerid, firstname, lastname;
-- 2. Select only the top 10 ranked customer from the previous question
WITH TotalRankedPurchases AS (
SELECT
i.customerid,
c.firstname,
c.lastname,
SUM(i.total) as total_sales,
RANK() OVER (ORDER BY SUM(i.total) DESC) as sales_rank
FROM
invoice i
JOIN
customer c ON i.customerid = c.customerid
GROUP BY customerid, firstname, lastname
)
SELECT *
FROM TotalRankedPurchases
WHERE sales_rank <= 10;
-- 3. Rank albums based on the total number of tracks sold.
SELECT
ar.Name,
a.title AS album_title,
SUM(il.quantity) AS total_tracks_sold,
DENSE_RANK() OVER (ORDER BY SUM(il.quantity) DESC) AS album_rank
FROM album a
JOIN artist ar USING (ArtistId)
JOIN track t ON a.albumid = t.albumid
JOIN invoiceline il ON t.trackid = il.trackid
GROUP BY a.albumid
ORDER BY album_rank;
-- 4. Do music preferences vary by country? What are the top 3 genres for each country?
WITH TopGenresPerCountry AS (
SELECT
i.BillingCountry AS Country,
g.Name AS Genre,
COUNT(il.TrackId) AS TrackCount,
RANK() OVER (PARTITION BY i.BillingCountry ORDER BY COUNT(il.TrackId) DESC) AS Ranking
FROM
InvoiceLine il
JOIN Track t USING (TrackId)
JOIN Genre g USING (Genreid)
JOIN Invoice i USING (InvoiceId)
GROUP BY Country, Genre
)
SELECT *
FROM TopGenresPerCountry
WHERE Ranking <= 3;
-- 5. In which countries is Blues the most popular genre?
WITH TopGenresPerCountry AS (
SELECT
i.BillingCountry AS Country,
g.Name AS Genre,
COUNT(il.TrackId) AS TrackCount,
RANK() OVER (PARTITION BY i.BillingCountry ORDER BY COUNT(il.TrackId)) AS Ranking
FROM
InvoiceLine il
JOIN Track t USING (TrackId)
JOIN Genre g USING (Genreid)
JOIN Invoice i USING (InvoiceId)
GROUP BY Country, Genre
)
SELECT Country
FROM TopGenresPerCountry
WHERE Ranking = 1
AND Genre = "Blues";
-- 6. Has there been year on year growth? By how much have sales increased per year?
SELECT
YEAR(InvoiceDate) AS Year_,
SUM(Total) AS YearTotalSales,
LAG(SUM(Total)) OVER (ORDER BY YEAR(InvoiceDate)) as PreviousYearTotal,
SUM(Total) - LAG(SUM(Total)) OVER (ORDER BY YEAR(InvoiceDate)) AS YearOnYearDifference
FROM invoice
GROUP BY YEAR(InvoiceDate);
-- 7. How do the sales vary month-to-month as a percentage?
SELECT
YEAR(InvoiceDate) AS Year_,
MONTH(InvoiceDate) AS Month_,
SUM(Total) AS MonthlyInvoiceTotals,
LAG(SUM(Total)) OVER (ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate)) AS PreviousMonthsInvoiceTotal,
ROUND(100 * (SUM(Total) - LAG(SUM(total)) OVER (ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) / (LAG(SUM(Total)) OVER (ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))), 2) AS SalesPercentageChange
FROM
invoice
GROUP BY MONTH(InvoiceDate), YEAR(InvoiceDate);
-- 8. What is the monthly sales growth, categorised by whether it was an increase or decrease compared to the previous month?
SELECT
YEAR(InvoiceDate) AS Sales_Year,
MONTH(InvoiceDate) AS Sales_Month,
SUM(Total) AS Sales_Amount,
(SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) AS Monthly_Sales_Growth,
CASE
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) > 0 THEN 'Increase'
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) < 0 THEN 'Decrease'
ELSE 'No Change'
END AS Growth_Category
FROM Invoices
GROUP BY YEAR(InvoiceDate), MONTH(InvoiceDate)
ORDER BY Sales_Year, Sales_Month;
-- 9. How many months in the data showed an increase in sales compared to the previous month?
WITH MonthlyGrowthCategorised AS (
SELECT
YEAR(InvoiceDate) AS Sales_Year,
MONTH(InvoiceDate) AS Sales_Month,
SUM(Total) AS Sales_Amount,
(SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) AS Monthly_Sales_Growth,
CASE
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) > 0 THEN 'Increase'
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) < 0 THEN 'Decrease'
ELSE 'No Change'
END AS Growth_Category
FROM Invoice
GROUP BY YEAR(InvoiceDate), MONTH(InvoiceDate)
ORDER BY Sales_Year, Sales_Month
)
SELECT COUNT(*) AS NumberOfIncreasedSalesMonths
FROM MonthlyGrowthCategorised
WHERE Growth_Category = "Increase";
-- 10. As a percentage of all months in the dataset, how many months in the data showed an increase in sales compared to the previous month?
WITH MonthlyGrowthCategorised AS (
SELECT
YEAR(InvoiceDate) AS Sales_Year,
MONTH(InvoiceDate) AS Sales_Month,
SUM(Total) AS Sales_Amount,
(SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) AS Monthly_Sales_Growth,
CASE
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) > 0 THEN 'Increase'
WHEN (SUM(Total) - LAG(SUM(Total)) OVER(ORDER BY YEAR(InvoiceDate), MONTH(InvoiceDate))) < 0 THEN 'Decrease'
ELSE 'No Change'
END AS Growth_Category
FROM Invoice
GROUP BY YEAR(InvoiceDate), MONTH(InvoiceDate)
ORDER BY Sales_Year, Sales_Month
)
SELECT
100
* (SELECT COUNT(*) FROM MonthlyGrowthCategorised WHERE Growth_Category = "Increase")
/ (SELECT COUNT(*) FROM MonthlyGrowthCategorised)
AS PercentOfMonthsWithIncrease;
-- 11. How have purchases of rock music changed quarterly? Show the quarterly change in the amount of tracks sold
SELECT
YEAR(i.InvoiceDate) AS Year_,
QUARTER(i.InvoiceDate) AS Quarter_,
COUNT(il.TrackID) AS QuarterPurchases,
LAG(COUNT(il.TrackID)) OVER (ORDER BY YEAR(i.InvoiceDate), QUARTER(i.InvoiceDate)) AS PreviousQuarter,
(COUNT(il.TrackID) - LAG(COUNT(il.TrackID)) OVER (ORDER BY YEAR(i.InvoiceDate), QUARTER(i.InvoiceDate))) AS QuarterlyChange
FROM
Invoice i
JOIN InvoiceLine il USING (InvoiceId)
JOIN Track t USING (TrackId)
JOIN Genre g USING (GenreId)
WHERE g.Name = 'Rock'
GROUP BY Year_, Quarter_
ORDER BY Year_, Quarter_;
-- 12. Determine the average time between purchases for each customer.
WITH PurchaseDates AS (
SELECT
CustomerId,
InvoiceDate,
LAG(InvoiceDate) OVER (PARTITION BY CustomerId ORDER BY InvoiceDate) AS PreviousInvoiceDate
FROM
Invoice
)
SELECT
CustomerId,
AVG(DATEDIFF(InvoiceDate, PreviousInvoiceDate)) AS AvgDaysBetweenPurchases
FROM
PurchaseDates
GROUP BY
CustomerId;