-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1322. Ads Performance.sql
More file actions
52 lines (46 loc) · 1.33 KB
/
1322. Ads Performance.sql
File metadata and controls
52 lines (46 loc) · 1.33 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
-- Write an SQL query to find the ctr of each Ad. Round ctr to two decimal points.
-- Return the result table ordered by ctr in descending order and by ad_id in ascending order in case of a tie.
-- Performance of the Ad is measured using Click-Through Rate (CTR)
--Solution1
SELECT ad_id,
IFNULL(ROUND(SUM(action='Clicked')/(SUM(action='Clicked')+SUM(action='Viewed'))*100, 2), 0) AS ctr
FROM Ads
GROUP BY ad_id
ORDER BY ctr DESC, ad_id ASC
--Solution2
WITH cte AS (
SELECT ad_id,
SUM(action = 'clicked') AS num_clicked,
SUM(action = 'viewed') AS num_viewed
FROM Ads
GROUP BY ad_id
)
SELECT ad_id,
IFNULL(ROUND(num_clicked/(num_clicked+num_viewed)*100,2),0) AS ctr
FROM cte
ORDER BY 2 DESC, 1 ASC;
/* Input:
Ads table:
+-------+---------+---------+
| ad_id | user_id | action |
+-------+---------+---------+
| 1 | 1 | Clicked |
| 2 | 2 | Clicked |
| 3 | 3 | Viewed |
| 5 | 5 | Ignored |
| 1 | 7 | Ignored |
| 2 | 7 | Viewed |
| 3 | 5 | Clicked |
| 1 | 4 | Viewed |
| 2 | 11 | Viewed |
| 1 | 2 | Clicked |
+-------+---------+---------+
Output:
+-------+-------+
| ad_id | ctr |
+-------+-------+
| 1 | 66.67 |
| 3 | 50.00 |
| 2 | 33.33 |
| 5 | 0.00 |
+-------+-------+ */