-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitch_sql_analysis.sql
More file actions
112 lines (92 loc) · 2.13 KB
/
twitch_sql_analysis.sql
File metadata and controls
112 lines (92 loc) · 2.13 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
-- viewing data table stream
SELECT *
FROM stream
LIMIT 20;
-- viewing data table chat
SELECT *
FROM chat
LIMIT 20;
-- finding all unique games in stream table
SELECT DISTINCT game
FROM stream;
-- finding all unique channels in stream table
SELECT DISTINCT channel
FROM stream;
-- finding game with most views
SELECT game, COUNT(*) as views
FROM stream
GROUP BY game
ORDER BY COUNT(*) DESC;
-- identifying League of Legend views by country
SELECT country, COUNT(*) as 'LOL viewers'
FROM stream
WHERE game = 'League of Legends'
GROUP BY country
ORDER BY COUNT(*) DESC;
-- identifying stream player/device with the most views
SELECT player, COUNT(*)
FROM stream
GROUP BY player
ORDER BY COUNT(*) DESC;
-- example of applying genres to certain games
SELECT game,
CASE
WHEN game = 'Dota 2'
THEN 'MOBA'
WHEN game = 'League of Legends'
THEN 'MOBA'
WHEN game = 'Heroes of the Storm'
THEN 'MOBA'
WHEN game = 'Counter-Strike: Global Offensive'
THEN 'FPS'
WHEN game = 'DayZ'
THEN 'Survival'
WHEN game = 'ARK: Survival Evolved'
THEN 'Survival'
ELSE 'Other'
END AS 'genre',
COUNT(*)
FROM stream
GROUP BY 1
ORDER BY 3 DESC;
-- viewing time data from stream table
SELECT time
FROM stream
LIMIT 10;
-- best time to stream in the US
SELECT strftime('%H', time),
COUNT(*)
FROM stream
WHERE country = 'US'
GROUP BY 1
order by 2 desc;
-- best local time to stream based on views globally
SELECT strftime('%H', time), channel, game, COUNT(*) as views
FROM stream
GROUP BY 1
ORDER BY COUNT(*) DESC
LIMIT 20;
-- combining chat and stream tables
WITH combined as
(
SELECT *
FROM stream
JOIN chat
ON stream.device_id = chat.device_id
)
SELECT *
from combined
limit 20;
-- utilizing window functions to gain insight on the number of views per player(device) by game and by channel(streamer)
SELECT
distinct channel,
game,
player,
count(device_id) OVER (
PARTITION BY player
order by game
) as 'count_views'
FROM
stream
where subscriber = 'true'
-- limit 20;