Skip to content

Commit a977d3d

Browse files
authored
Create ads-performance.sql
1 parent 1eaf1f3 commit a977d3d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

MySQL/ads-performance.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
SELECT ad_id,
5+
CASE
6+
WHEN clicks + views = 0 THEN 0
7+
ELSE ROUND(100 * clicks / (clicks + views), 2)
8+
END ctr
9+
FROM
10+
(SELECT ad_id,
11+
SUM(CASE
12+
WHEN action ='Viewed' THEN 1
13+
ELSE 0
14+
END) views,
15+
SUM(CASE
16+
WHEN action = 'Clicked' THEN 1
17+
ELSE 0
18+
END) clicks
19+
FROM Ads
20+
GROUP BY ad_id) a
21+
ORDER BY ctr DESC,
22+
ad_id ASC
23+

0 commit comments

Comments
 (0)