-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path05-laptop-vs-mobile-viewership.sql
52 lines (34 loc) · 1.73 KB
/
05-laptop-vs-mobile-viewership.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
-- Assume you're given the table on user viewership categorised by device type where the three types are laptop, tablet, and phone.
-- Write a query that calculates the total viewership for laptops and mobile devices where mobile is defined as the sum of tablet and phone viewership. Output the total viewership for laptops as laptop_reviews and the total viewership for mobile devices as mobile_views.
-- Solution 1: using [CASE WHEN THEN ELSE]
SELECT Sum(CASE
WHEN device_type = 'laptop' THEN 1
ELSE 0
END) AS laptop_views,
Sum(CASE
WHEN device_type IN ( 'tablet', 'phone' ) THEN 1
ELSE 0
END) AS mobile_views
FROM viewership;
-- Solution 2 : using FILTER (didnt knew FILTER existed)
SELECT Count(*) filter( WHERE device_type='laptop') AS laptop_views,
Count(*) filter( WHERE device_type IN('tablet', 'phone')) AS mobile_views
FROM viewership;
-- Solution 3: using JOINs (wolud not have thought)
SELECT Count(DISTINCT a.user_id) AS "laptop_views",
Count(DISTINCT b.user_id) AS "mobile_views"
FROM viewership AS a
INNER JOIN viewership AS b
ON a.device_type = 'laptop'
AND b.device_type IN ( 'tablet', 'phone' )
-- first approach: was stuck and couldn't get ahead. should have used SUM() instead of COUNT()
SELECT COUNT(CASE
WHEN device_type = 'laptop'
END) AS laptop_views,
COUNT(CASE
WHEN device_type IN ( 'tablet', 'phone' )
END) AS mobile_views
FROM viewership;
-- REMARKS :
-- 1. didn't get the idea to use 'THEN 1 ELSE 0' --> makes it very easy to solve.
-- 2. also can use NOT IN('laptop') everywhere instead of, IN ('tablet', 'phone')