|
1 |
| ---1322. Ads Performance |
2 |
| --- |
3 |
| ---Table: Ads |
4 |
| --- |
5 |
| ---+---------------+---------+ |
6 |
| ---| Column Name | Type | |
7 |
| ---+---------------+---------+ |
8 |
| ---| ad_id | int | |
9 |
| ---| user_id | int | |
10 |
| ---| action | enum | |
11 |
| ---+---------------+---------+ |
12 |
| ---(ad_id, user_id) is the primary key for this table. |
13 |
| ---Each row of this table contains the ID of an Ad, the ID of a user and the action taken by this user regarding this Ad. |
14 |
| ---The action column is an ENUM type of ('Clicked', 'Viewed', 'Ignored'). |
15 |
| --- |
16 |
| --- |
17 |
| ---A company is running Ads and wants to calculate the performance of each Ad. |
18 |
| --- |
19 |
| ---Performance of the Ad is measured using Click-Through Rate (CTR) where: |
20 |
| --- |
21 |
| --- |
22 |
| --- |
23 |
| ---Write an SQL query to find the ctr of each Ad. |
24 |
| --- |
25 |
| ---Round ctr to 2 decimal points. Order the result table by ctr in descending order and by ad_id in ascending order in case of a tie. |
26 |
| --- |
27 |
| ---The query result format is in the following example: |
28 |
| --- |
29 |
| ---Ads table: |
30 |
| ---+-------+---------+---------+ |
31 |
| ---| ad_id | user_id | action | |
32 |
| ---+-------+---------+---------+ |
33 |
| ---| 1 | 1 | Clicked | |
34 |
| ---| 2 | 2 | Clicked | |
35 |
| ---| 3 | 3 | Viewed | |
36 |
| ---| 5 | 5 | Ignored | |
37 |
| ---| 1 | 7 | Ignored | |
38 |
| ---| 2 | 7 | Viewed | |
39 |
| ---| 3 | 5 | Clicked | |
40 |
| ---| 1 | 4 | Viewed | |
41 |
| ---| 2 | 11 | Viewed | |
42 |
| ---| 1 | 2 | Clicked | |
43 |
| ---+-------+---------+---------+ |
44 |
| ---Result table: |
45 |
| ---+-------+-------+ |
46 |
| ---| ad_id | ctr | |
47 |
| ---+-------+-------+ |
48 |
| ---| 1 | 66.67 | |
49 |
| ---| 3 | 50.00 | |
50 |
| ---| 2 | 33.33 | |
51 |
| ---| 5 | 0.00 | |
52 |
| ---+-------+-------+ |
53 |
| ---for ad_id = 1, ctr = (2/(2+1)) * 100 = 66.67 |
54 |
| ---for ad_id = 2, ctr = (1/(1+2)) * 100 = 33.33 |
55 |
| ---for ad_id = 3, ctr = (1/(1+1)) * 100 = 50.00 |
56 |
| ---for ad_id = 5, ctr = 0.00, Note that ad_id = 5 has no clicks or views. |
57 |
| ---Note that we don't care about Ignored Ads. |
58 |
| ---Result table is ordered by the ctr. in case of a tie we order them by ad_id |
59 |
| - |
60 | 1 | --# Write your MySQL query statement below
|
61 | 2 | select ad_id,
|
62 | 3 | ifnull(
|
|
0 commit comments