-
Notifications
You must be signed in to change notification settings - Fork 0
/
Milestone_3
109 lines (89 loc) · 2.51 KB
/
Milestone_3
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
-- Milestone 3
-- This document has solutions to 6 out of the 10 Questions (Q2,3,6,7,8,10) mentioned in Milestone 2.
-- Question 2: What is the most common age range among consumers who have registered on YouTube in the last six months?
SELECT Age, COUNT(*) AS Count
FROM (
SELECT
CASE
WHEN Age BETWEEN 0 AND 18 THEN '0-18'
WHEN Age BETWEEN 19 AND 30 THEN '19-30'
WHEN Age BETWEEN 31 AND 40 THEN '31-40'
WHEN Age BETWEEN 41 AND 50 THEN '41-50'
WHEN Age BETWEEN 51 AND 60 THEN '51-60'
WHEN Age > 60 THEN '60+'
ELSE 'Unknown'
END AS Age
FROM Consumer
WHERE RegistrationID >= ADD_MONTHS(SYSDATE, -6)
) Age
GROUP BY Age
ORDER BY Count DESC;
Question 3: Which channel type has the highest average number of video views per video?
WITH MaxViewPerCategory AS (
SELECT
categoryid,
MAX(view_count) AS max_views
FROM
YTvideo
GROUP BY
categoryid
)
SELECT
AVG(max_views) AS avg_highest_views
FROM
MaxViewPerCategory;
-- Question 6: Which YouTube category has the highest ratio of likes to dislikes on average?
WITH LikesDislikesRatio AS (
SELECT
Category,
AVG(Likes) AS AvgLikes,
AVG(Dislikes) AS AvgDislikes,
CASE
WHEN AVG(Dislikes) = 0 THEN NULL
ELSE AVG(Likes) / AVG(Dislikes)
END AS LikesToDislikesRatio
FROM
YTVideo
GROUP BY
Category
)
SELECT
Category,
AVG(LikesToDislikesRatio) AS AvgLikesToDislikesRatio
FROM
LikesDislikesRatio
GROUP BY
Category
ORDER BY
AvgLikesToDislikesRatio DESC;
-- Question 7: What is the average video length that a person watches on YouTube?
SELECT AVG(Duration) AS AvgVideoLength
FROM YTVIDEO;
-- Question 8: Which countries/regions contribute the most subscribers for a given channel?
SELECT
ChannelName,
Country,
COUNT(ConsumerID) AS SubscribersCount
FROM
Channel c
JOIN
Consumer con ON c.ChannelName = con.ConsumerName
GROUP BY
ChannelName, Country
ORDER BY
SubscribersCount DESC;
-- Question 10: What is the correlation between the number of subscribers a channel has and the number of views its videos receive on average?
WITH ChannelStats AS (
SELECT
ChannelName,
SubscribersCount,
AVG(VideoViews) AS AvgVideoViews
FROM
Channel
GROUP BY
ChannelName, SubscribersCount
)
SELECT
CORR(SubscribersCount, AvgVideoViews) AS Correlation
FROM
ChannelStats;