-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXYZ Ads Airing Query.sql
More file actions
500 lines (200 loc) · 6.29 KB
/
XYZ Ads Airing Query.sql
File metadata and controls
500 lines (200 loc) · 6.29 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
--ALTER TABLE DW.ADS_AIRING
--DROP COLUMN Broadcast_month
alter table dw.ads_airing
add Duration_Hrs float(50)
update dw.ads_airing
set Duration_hrs= Round((Duration/3600),2)
Alter table DW.ADS_AIRING
add Broadcast_Month char(250)
update dw.Ads_Airing
set Quarter= Case when Date between '01/01/2021' AND'03/31/2021' THEN 'Q1'
WHEN Date between '04/01/2021' AND '06/30/2021' THEN 'Q2'
WHEN Broadcast_Month between '07/01/2021' AND '09/30/2021' THEN 'Q3'
ELSE 'Q4'
END
update dw.Ads_Airing
set Broadcast_Month= Case when Date between '01/01/2021' AND'01/31/2021' THEN 'JAN'
WHEN Date between '02/01/2021' AND '02/28/2021' THEN 'FEB'
WHEN DATE between '03/01/2021' AND '03/31/2021' THEN 'MAR'
WHEN DATE between '04/01/2021' AND '04/30/2021' THEN 'APR'
WHEN DATE between '05/01/2021' AND '05/31/2021' THEN 'MAY'
WHEN DATE between '06/01/2021' AND '06/30/2021' THEN 'JUN'
WHEN DATE between '07/01/2021' AND '07/31/2021' THEN 'JUL'
WHEN DATE between '08/01/2021' AND '08/31/2021' THEN 'AUG'
WHEN DATE between '09/01/2021' AND '09/30/2021' THEN 'SEP' ` +
WHEN DATE between '10/01/2021' AND '10/31/2021' THEN 'OCT'
WHEN DATE between '11/01/2021' AND '11/30/2021' THEN 'NOV'
WHEN DATE between '12/01/2021' AND '12/31/2021' THEN 'DEC'
END
--Check
SELECT DISTINCT DATE , Broadcast_Month, Quarter FROM DW.Ads_Airing
WHERE Broadcast_Month='DEC'
ORDER BY 1
--Correct
update dw.Ads_Airing
set quarter = 'Q3'
WHERE cast(date as date) between '07/01/2021' AND '09/30/2021'
update dw.Ads_Airing
set Broadcast_Year=2021 where
Broadcast_Year=2022
--What is Pod Position? Does the Pod position number affect the amount spent on Ads for a specific period of time by a company?
SELECT * FROM
(
SELECT BRAND , Pod_Position, [Spend ] FROM DW.Ads_Airing
) A
pivot
( SUM ([Spend ]) for Pod_Position IN ([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]
)
) b
order by 1,2
--What is the share of various brands in TV airings and how has it changed from Q1 to Q4 in 2021?
SELECT Brand, Round(cast(count(id) *100 as float ) / sum(count(ID)) OVER (), 2) as 'Percentage'
FROM dw.Ads_Airing
where Quarter='Q4'
GROUP BY Brand
order by 1
--Total Quarterly Spend Shares by Companies in a Year
select * from
(
Select distinct BRAND,sum([Spend]) OVER (PARTITION BY BRAND) / sum([spend]) over () as Share from dw.Ads_Airing
where quarter='Q4'
) a
order by 1
--Conduct a competitive analysis for the brands and define advertisement strategy of different brands and how it differs across the brands.
-- Duration Ads Aired on DayParts
select * from
(
Select brand, [Duration_Hrs] , Dayparts from dw.Ads_Airing
) a
pivot
(
sum ([Duration_Hrs]) for dayparts in ([WEEKEND] , [EARLY FRINGE], [EARLY MORNING], [EVENING NEWS],
[LATE FRINGE], [OVERNIGHT],[PRIME ACCESS],
[PRIME TIME], [DAYTIME])
)B
-- All Key Metrics
select a.*, sum([Total Ads Per Product]) over ( partition by brand) as [Total Ads Per Brand]
from
(
select Brand, Product, count(id) as [Total Ads Per Product], round( sum(EQ_Units), 1) TotaSales ,
sum(spend) as TotalSpend from dw.Ads_Airing
group by brand, product
)a
-- Duration of the Ads Per Pod Poisition in Hours
SELECT * FROM
(
SELECT Brand, Pod_Position, Duration_hrs FROM DW.Ads_Airing
) A
pivot
(
SUM(Duration_hrs) for Pod_Position IN ([1]
,[2]
,[3]
,[4]
,[5]
,[6]
,[7]
,[8]
,[9]
,[10]
)
) b
-- Profit Yield Per Brand
select Brand,[Total Sales],[Total Spend], [Total Ads],[Total Sales]/[Total Ads] AS [Profit Yield] ,
Round([Total Sales]/[Total Spend],4) AS [Spend Yield]
from
(
select Brand, count(id) as [Total Ads], round( sum(EQ_Units), 1) [Total Sales] ,Sum ([Spend ]) as [Total Spend]
from dw.Ads_Airing
GROUP BY Brand
) A
order by 2 desc
-- By Network Type
Select Brand, Network_Type , count(id) as Ads from dw.ads_airing
group by brand, Network_Type
order by Brand , 3
--Mahindra and Mahindra wants to run a digital ad campaign to complement its existing TV ads in Q1 of 2022.
--Based on the data from 2021, suggest a media plan to the CMO of Mahindra and Mahindra. Which audience should they target?
--*Assume XYZ Ads has the ad viewership data and TV viewership for the people in India.
--Profit Yeild
select * from
(
Select Brand, ROUND(sum([EQ_Units]) / sum([Spend ]), 5) as Profit_Yield from dw.Ads_Airing
where quarter='Q4' and Brand ='Mahindra and Mahindra'
group by BRand
) a
order by 1
-- Duration of the Ads Per Pod Poisition in Hours
SELECT * FROM
(
SELECT Brand, qUARTER, Duration_hrs FROM DW.Ads_Airing
where Brand ='Mahindra and Mahindra'
) A
pivot
(
SUM(Duration_hrs) for qUARTER IN ([Q1]
,[Q2]
,[Q3]
,[Q4]
)
) b
SELECT * FROM
(
SELECT Brand, qUARTER, [SPEND] FROM DW.Ads_Airing
where Brand ='Mahindra and Mahindra'
) A
pivot
(
SUM([SPEND]) for qUARTER IN ([Q1]
,[Q2]
,[Q3]
,[Q4]
)
) b
-- Duration Ads Aired on DayParts
select * from
(
Select brand, [Duration_Hrs] , Dayparts from dw.Ads_Airing
where Brand ='Mahindra and Mahindra' and quarter='Q4'
) a
pivot
(
sum ([Duration_Hrs]) for dayparts in ([WEEKEND] , [EARLY FRINGE], [EARLY MORNING], [EVENING NEWS],
[LATE FRINGE], [OVERNIGHT],[PRIME ACCESS],
[PRIME TIME], [DAYTIME])
)B
-- By product
Select Brand, Product , count(id) as Ads from dw.ads_airing
where Brand ='Mahindra and Mahindra'
group by brand, Product
order by Brand , 3