-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrhythmic_query.sql
More file actions
323 lines (294 loc) · 7.06 KB
/
rhythmic_query.sql
File metadata and controls
323 lines (294 loc) · 7.06 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use rhythmic_query;
show tables;
# what genres of music are most popular?
# For this purpose, we need to lookup for three tables, genre, track and invoiceline.
select g.GenreId, g.Name, sum(i.Quantity) quantities_sold from
genre g
join
track t
on g.GenreId = t.GenreId
join
invoiceline i
on
t.TrackId = i.TrackId
group by
g.GenreId
order by
sum(i.Quantity)
desc
limit 5;
# The above query extracts top three Genre which are most popular by the number of their quantities sold.
# In this, we see Rock having the most number of copies sold.
# which artist are popular by week, month, year?
# Tables used artist, album, track. invoiceline
select a.Name, sum(i.Quantity) quantities_sold, Month(inv.InvoiceDate) `month` from
artist a
join
album ab
on
a.ArtistId = ab.ArtistId
join
track t
on
ab.AlbumId = t.AlbumId
join
invoiceline i
on
t.TrackId = i.TrackId
join
invoice inv
on
i.InvoiceId = inv.InvoiceId
group by
a.ArtistId, Month(inv.InvoiceDate)
order by
sum(i.Quantity)
desc
limit 5;
# By week
select a.Name, sum(i.Quantity) quantities_sold, Week(inv.InvoiceDate) as `week` from
artist a
join
album ab
on
a.ArtistId = ab.ArtistId
join
track t
on
ab.AlbumId = t.AlbumId
join
invoiceline i
on
t.TrackId = i.TrackId
join
invoice inv
on
i.InvoiceId = inv.InvoiceId
group by
a.ArtistId, Week(inv.InvoiceDate)
order by
sum(i.Quantity)
desc
limit 5;
# By Year
select a.Name, sum(i.Quantity) quantities_sold, Year(inv.InvoiceDate) as `year` from
artist a
join
album ab
on
a.ArtistId = ab.ArtistId
join
track t
on
ab.AlbumId = t.AlbumId
join
invoiceline i
on
t.TrackId = i.TrackId
join
invoice inv
on
i.InvoiceId = inv.InvoiceId
group by
a.ArtistId, Year(inv.InvoiceDate)
order by
sum(i.Quantity)
desc
limit 5;
# What can you tell me about the customers that are spending the most on [genre], [artist], etc?
WITH GenreSpending AS (
SELECT
g.GenreId,
g.Name AS Genre,
c.CustomerId,
concat(c.FirstName, ' ', c.LastName) AS CustomerName,
SUM(il.UnitPrice * il.Quantity) AS TotalSpending
FROM
Customer c
JOIN
Invoice i ON c.CustomerId = i.CustomerId
JOIN
InvoiceLine il ON i.InvoiceId = il.InvoiceId
JOIN
Track t ON t.TrackId = il.TrackId
JOIN
Genre g ON g.GenreId = t.GenreId
GROUP BY
g.GenreId, c.CustomerId
),
RankedSpending AS (
SELECT
GenreId,
Genre,
CustomerId,
CustomerName,
TotalSpending,
RANK() OVER (PARTITION BY GenreId ORDER BY TotalSpending DESC) AS SpendingRank
FROM
GenreSpending
)
SELECT
Genre,
CustomerName AS TopSpender,
TotalSpending
FROM
RankedSpending
WHERE
SpendingRank = 1;
-- 13.Track changes in sales by genre using the genre, track, and invoiceline tables to identify trends.
-- Tableau Question
-- select * from
-- Genre g
-- Join
-- track t
-- on
-- t.GenreId = g.GenreId
-- join invoiceline it
-- on
-- it.TrackId = t.TrackId;
-- 14 How effective are different media types in generating sales?
select m.name, sum(it.UnitPrice * it.Quantity) TotalSales from track t
join
mediatype m
on
t.MediaTypeId = m.MediaTypeId
join
invoiceline it
on
t.TrackId = it.TrackId
group by
m.MediaTypeId
order by
TotalSales
desc;
# MPEG audio file is the mostly used media type which has maximum sale equal to 1956.24 units.
-- 16.What is the average customer purchase size and frequency?
-- 17.Use data from the customer and invoice tables to calculate average invoice totals and purchase frequency per customer.
select concat(c.FirstName, ' ', c.LastName) Customer, sum(i.Total) invoice_total, count(*) purchase_frequency
from Customer c
Join Invoice i
on
c.CustomerId = i.CustomerId
group by
c.CustomerId
order by
invoice_total
desc;
# Dummy data and thus is uniform for each customer
# From the data, it can be inferred that Helena Holy made the most purchase in her 7 total visits.
-- 18.Which playlists are most popular among users?
with playlistInfo as (
select sum(il.Quantity) quantity, p.playlistid playlistid, p.Name playlist from
Customer c
join invoice i
on
c.CustomerId = i.CustomerId
Join invoiceline il
on
i.InvoiceId = il.InvoiceId
Join track t
on
il.TrackId = t.TrackId
Join playlisttrack pt
on
pt.TrackId = t.TrackId
Join playlist p
on
p.PlaylistId = pt.PlaylistId
group by
p.PlaylistId
order by
quantity
desc
)
-- select distinct p1.playlist, p1.playlistId, case
-- when p1.playlist = p2.playlist then p1.quantity + p2.quantity
-- end as quantity from playlistinfo p1
-- join playlistinfo p2
-- on
-- p1.playlistid = p2.playlistid;
# As we see some of the playlist have same names but different ids
# Assuming the different playlist id correspond to the same data.
select playlist ,sum(quantity) quantity from playlistinfo group by playlist;
-- select * from (select sum(quantity) quantity, row_number() over(partition by playlist order by playlistid) as pl_rownum from playlistinfo group by playlist) as grouped_table where pl_rownum=1
-- -- , sum(quantity) quantity from playlistinfo group by playlist;
# From the result, it can be inferred that Music playlist is the most popular among users.
-- 19.Analyze data from the playlist and playlisttrack tables to determine which playlists have the most tracks or engagement.
select count(*) track_count, pt.playlistId from playlist p
join
playlisttrack pt
on
p.PlayListid - pt.playlistid
join track t
on
t.TrackId = pt.TrackId
group by
pt.playlistId
order by
track_count
desc;
-- 20.How does customer location influence purchasing behavior?
# Visualizing customer city's effect on purchasing behavior
select c.city, sum(i.Total) total_purchase from
Customer c
Join
invoice i
on
c.CustomerId = i.CustomerId
group by
c.city
order by
total_purchase
desc;
# Prague (capital of Czech Republic) is the city in which customer mostly purchase
select c.state, sum(i.Total) total_purchase from
Customer c
Join
invoice i
on
c.CustomerId = i.CustomerId
where c.state is not null
group by
c.state
order by
total_purchase
desc;
# As we see California being the state with most purchases followed by SP (São Paulo).
select c.Country, sum(i.Total) total_purchase from
Customer c
Join
invoice i
on
c.CustomerId = i.CustomerId
where c.Country is not null
group by
c.Country
order by
total_purchase
desc;
# As we saw state CA has the maximum purchasing behavior and so country wise we found USA to be having the most purchasing behavior.
-- 22.What is the impact of employee support on customer satisfaction?
-- 23.Explore any correlations between customer spending and their assigned support representative using data from the customer and employee tables.
with employee_customer as (
select concat(e.FirstName, " ", e.LastName) employee_name, e.EmployeeId, c.CustomerId, max(i.Total) as total_spend from
Customer c
Join
Employee e
on
c.SupportRepId = e.EmployeeId
Join
Invoice i
on
c.CustomerId = i.CustomerId
group by
c.CustomerId
)
select employee_name, employeeid, sum(total_spend) tot_spend from
employee_customer
group by
employeeId
order by
tot_spend
desc;
# Jane Peacock is considered the best support representative with maximum interactions with customers
# and a total spend of $313.11.