Skip to content

Commit 3f2c20c

Browse files
add 1322
1 parent 2e4a676 commit 3f2c20c

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ _If you like this project, please leave me a star._ ★
851851
| # | Title | Solutions | Video | Difficulty | Tag
852852
|-----|----------------|---------------|---------------|---------------|-------------
853853
|1327|[List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)|[Solution](../master/database/_1327.sql) || Easy |
854+
|1322|[Ads Performance](https://leetcode.com/problems/ads-performance/)|[Solution](../master/database/_1322.sql) || Easy |
854855
|1294|[Weather Type in Each Country](https://leetcode.com/problems/weather-type-in-each-country/)|[Solution](../master/database/_1294.sql) | | Easy |
855856
|1280|[Students and Examinations](https://leetcode.com/problems/students-and-examinations/)|[Solution](../master/database/_1280.sql) | [:tv:](https://www.youtube.com/watch?v=ThbkV4Fs7iE)| Easy |
856857
|1251|[Average Selling Price](https://leetcode.com/problems/average-selling-price/)|[Solution](../master/database/_1251.sql) | | Easy |

database/_1322.sql

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
--# Write your MySQL query statement below
61+
select ad_id,
62+
ifnull(
63+
round(
64+
avg(
65+
case
66+
when action = "Clicked" then 1
67+
when action = "Viewed" then 0
68+
else null
69+
end
70+
) * 100,
71+
2),
72+
0)
73+
as ctr
74+
from Ads
75+
group by ad_id
76+
order by ctr desc, ad_id asc;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy